`Query`类型的未知字段`edge`

时间:2018-03-26 02:23:28

标签: django relayjs graphene-python

当我尝试在项目中运行relay-compiler时,我从标题中收到错误。我使用graphql-python / graphene-django作为后端graphql服务器。这是我的架构的缩写副本。

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)

通过该设置,我可以在GraphiQL中成功运行以下查询

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

到目前为止一切顺利,但现在我正在尝试构建一个可以使用此查询的客户端组件。

jsapp /组件/ 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>;
        }}
      />
    );
  }
}

查询是相同的,但是当我运行relay-compiler --src ./jsapp --schema ./schema.graphql --extensions js jsx时,我收到错误:

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

如果我使用Django .json管理命令生成的graphaql_schema架构或.graphql检索到的get-graphql-schema架构,则会收到此错误。

我错过了哪一部分?

1 个答案:

答案 0 :(得分:1)

我可能会有点迟到,但在我看来,你正试图在模式的根上寻求边缘,片段:

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

我认为可以改为:

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

关键字query后面的文字是指&#39;查询的名称,而不是架构的根。如果您正在使用Relay Modern(通过使用QueryRenderer,我假设您是),那么编译器应该对您大喊大叫,将片段的名称修改为反映其存储的文件名的内容。