GraphQL nested query definition

CorwinCZ picture CorwinCZ · Jan 31, 2017 · Viewed 26.6k times · Source

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.

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. 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.

I'm using node.js + express + graphql-js on my server.

Answer

Shalkam picture Shalkam · Feb 3, 2017

Short answer:
type should be a GraphQLObjectType containing all the fields like this:

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

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

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

and finally my schema schema.js that is passed to graphql express server

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
  })
});