简体   繁体   English

GraphQL嵌套查询定义

[英]GraphQL nested query definition

I'm trying to create tree-like structure for my queries, to get rid off queries like 我正在尝试为我的查询创建树状结构,以摆脱像我这样的查询

peopleList, peopleSingle, peopleEdit, peopleAdd, peopleDelete companyList, companySingle, companyEdit, companyAdd, companyDelete etc. 

In the end I would like to send query like this: 最后我想发送这样的查询:

query test {
  people {
    list {
      id
      name
    }
    single(id: 123) {
      id
      name
    }
  }
  company {
    list {
      id
      name
    }
    single(id: 456) {
      id
      name
    }
  }
}

mutation test2 {
  people {
    create(data: $var) {
      id
      name
    }
  }
  people {
    edit(id: 123, data: $var) {
      id
      name
    }
  }
}

This is part of my query object on people module: 这是我的人员模块上的查询对象的一部分:

people: {
  type: //What type this should be?
  name: 'Root of People queries',
  fields: () => ({
    list: {
      type: peopleType,
      description: 'Returns all people in DB.',
      resolve: () => {
        // resolve method implementation
      }
    },
    single: {
      type: peopleType,
      description: 'Single row from people table. Requires ID argument.',
      args: {
        id: { type: new GraphQLNonNull(GraphQLID) }
      },
      resolve: () => {
        // resolve method implementation
      }
    }
  })
}

I have tried to put this snippet into GraphQLObjectType and then combine them together in RootQuery (using GraphQLObjectType again) - didn't work. 我试图将这个片段放入GraphQLObjectType,然后在RootQuery中将它们组合在一起(再次使用GraphQLObjectType) - 不起作用。

Alternative method could be to create new Type - like peopleQueriesType, inside this type specify all my queries as fields and then create single query for this object. 替代方法可以是创建新类型 - 类似peopleQueriesType,在此类型中将所有查询指定为字段,然后为此对象创建单个查询。 But this seems odd to me - polluting my code with unnecessary objects just to merge my queries in tree-like shape. 但这对我来说似乎很奇怪 - 用不必要的对象污染我的代码只是为了将我的查询合并成树状。

I have tried to look at Apollo server implementation, if it can do this kind of query structure, but couldn't find any help in documentation. 我试图看看Apollo服务器实现,如果它可以做这种查询结构,但在文档中找不到任何帮助。

I'm using node.js + express + graphql-js on my server. 我在我的服务器上使用node.js + express + graphql-js。

Short answer: 简短回答:
type should be a GraphQLObjectType containing all the fields like this: type应该是包含所有字段的GraphQLObjectType ,如下所示:

type: new GraphQLObjectType({ name: 'patientQuery', fields: { find, findOne } })

Details: I ended up with this query using the code below: 详细信息:我最终使用以下代码进行了此查询:

{
  patient {
    find {
      id
      active
    }
    findOne(id: "pat3") {
      id
      active
    }
  }
}

in patient/queries/index.js I have this patient/queries/index.js我有这个

import findOne from './find-one.js';
import find from './find.js';
import { GraphQLObjectType } from 'graphql';

export default {
  patient: {
    type: new GraphQLObjectType({ name: 'patientQuery', fields: { find, findOne } }),
    resolve(root, params, context, ast) {
      return true;
    }
  }
};

then in queries.js 然后在queries.js

import patient from './patient/queries/index.js';
export default {
  ...patient
};

and finally my schema schema.js that is passed to graphql express server 最后我的架构schema.js传递给graphql express服务器

import {
  GraphQLObjectType,
  GraphQLSchema
} from 'graphql';

import queries from './queries';
import mutations from './mutations';

export default new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'Query',
    fields: queries
  }),
  mutation: new GraphQLObjectType({
    name: 'Mutation',
    fields: mutations
  })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM