Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 212
CorwinCZ Avatar asked Jan 31 '17 09:01

CorwinCZ


People also ask

How do you query nested objects in GraphQL?

When setting up a field whose value is a custom type, we have to define a function that tells GraphQL how to get that custom type. In our case, we want to tell GraphQL how to get the posts if we have the author. We do that by defining a new root property inside resolvers.

What is query in GraphQL?

A GraphQL query is used to read or fetch values while a mutation is used to write or post values. In either case, the operation is a simple string that a GraphQL server can parse and respond to with data in a specific format.

What is __ Typename in GraphQL?

The __typename field returns the object type's name as a String (e.g., Book or Author ). GraphQL clients use an object's __typename for many purposes, such as to determine which type was returned by a field that can return multiple types (i.e., a union or interface).


1 Answers

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
  })
});
like image 104
Shalkam Avatar answered Oct 23 '22 12:10

Shalkam