graphql, union scalar type?

Scalars can’t be used as part of unions, since per the specification, unions specifically “represent an object that could be one of a list of GraphQL Object types.” Instead, you can use a custom scalar. For example: const MAX_INT = 2147483647 const MIN_INT = -2147483648 const coerceIntString = (value) => { if (Array.isArray(value)) { throw … Read more

GraphQL mutation: Invariant Violation: Must contain a query definition

You should use the mutate method of the client for mutations, not the query method. The options for the method can be found in the docs. Apollo is opinionated about how queries and mutations are treated, so each method has different options that are appropriate to each operation’s behavior (for example, mutate includes a refetchQueries … Read more

GraphQL queries with tables join using Node.js

The concept you are refering to is called batching. There are several libraries out there that offer this. For example: Dataloader: generic utility maintained by Facebook that provides “a consistent API over various backends and reduce requests to those backends via batching and caching” join-monster: “A GraphQL-to-SQL query execution layer for batch data fetching.”

How to load a .graphql file using `apollo-server`?

If you define your type definitions inside a .graphql file, you can read it in one of several ways: 1.) Read the file yourself: const { readFileSync } = require(‘fs’) // we must convert the file Buffer to a UTF-8 string const typeDefs = readFileSync(require.resolve(‘./type-defs.graphql’)).toString(‘utf-8’) 2.) Utilize a library like graphql-tools to do it for … Read more

How to get requested fields inside GraphQL resolver?

In graphql-js resolvers expose a fourth argument called resolve info. This field contains more information about the field. From the GraphQL docs GraphQLObjectType config parameter type definition: // See below about resolver functions. type GraphQLFieldResolveFn = ( source?: any, args?: {[argName: string]: any}, context?: any, info?: GraphQLResolveInfo ) => any type GraphQLResolveInfo = { fieldName: … Read more

How do I handle deletes in react-apollo

I am not sure it is good practise style but here is how I handle the deletion of an item in react-apollo with updateQueries: import { graphql, compose } from ‘react-apollo’; import gql from ‘graphql-tag’; import update from ‘react-addons-update’; import _ from ‘underscore’; const SceneCollectionsQuery = gql ` query SceneCollections { myScenes: selectedScenes (excludeOwner: false, … Read more

GraphQLError: Query root type must be provided

All servers running with GraphQL must have at least one @Query() to be considered a valid GraphQL server. Without it, the apollo-server package will throw an exception and the server will fail to start. This can be as simple as @Resolver() export class FooResolver { @Query(() => String) sayHello(): string { return ‘Hello World!’; } … Read more