Search code examples
djangorelayjsgraphene-python

Unknown field `edges` on type `Query`


I am getting the error from the title when I attempt to run relay-compiler in my project. I am using graphql-python/graphene-django for the back end graphql server. Here's an abbreviated copy of my schema.

grove.gql_schema.py:

from graphene_django import DjangoObjectType
from graphene_django.filter import DjangoFilterConnectionField

from .models import Tree

class TreeNode(DjangoObjectType):
    class Meta:
        model = Tree
        filter_fields = ['owner']
        interfaces = (relay.Node,)

class Query(ObjectType):

    def resolve_my_trees(self, info):
        if not info.context.user.is_authenticated:
            return Tree.objects.none()
        else:
            return Tree.objects.filter(owner=info.context.user)
            my_trees = DjangoFilterConnectionField(TreeNode)
            tree = relay.Node.Field(TreeNode)

project.gql_schema:

class Query(grove.gql_schema.Query, graphene.ObjectType):
    pass

schema = graphene.Schema(query=Query)

With that setup, I can successfully run the following query in GraphiQL

query   {
  myTrees {
    edges {
      node {
        id
      }
    }
  }
}

So far so good, but now I'm trying to build a client-side component that can use this query.

jsapp/components/index.jsx:

import React from 'react';
import {graphql, QueryRenderer} from 'react-relay';

import environment from '../relay_env'

const myTreesQuery = graphql`
  query componentsMyTreesQuery {
    edges {
      node {
        id
      }
    }
  }
`;

export default class App extends React.Component {
  render() {
    return (
      <QueryRenderer
        environment={environment}
        query={myTreesQuery}
        variables={{}}
        render={({error, props}) => {
          if (error) {
            return <div>Error!</div>;
          }
          if (!props) {
            return <div>Loading...</div>;
          }
          return <div>User ID: {props.edges}</div>;
        }}
      />
    );
  }
}

The query is identical, but when I run relay-compiler --src ./jsapp --schema ./schema.graphql --extensions js jsx I get the error:

GraphQLParser: Unknown field `edges` on type `Query`.
Source: document `componentsMyTreesQuery` file: `components/index.jsx`.

I get this error if when I use the .json schema generated by the Django graphaql_schema management command or the .graphql one retrieved by get-graphql-schema.

What part am I missing?


Solution

  • I might be a bit late but it looks to me like you're trying to ask for the edges on the root of your schema, the fragment:

    const myTreesQuery = graphql`
      query componentsMyTreesQuery {
        edges {
          node {
            id
          }
        }
      }
    `;
    

    I think could be altered to:

    const myTreesQuery = graphql`
      query componentsMyTreesQuery {
        myTrees {
          edges {
            node {
              id
            }
          }
        }
      }
    `;
    

    The text after the keyword query refers to the name of the 'query', not the root of the schema. If you're using Relay Modern (which by the use of QueryRenderer, I assume you are) then the compiler should yell at you to fix the name of your fragment to something that reflects the filename that it's stored in.