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

React Apollo Error: Invariant Violation: Could not find “client” in the context or passed in as an option

In my case, I found that I had react-apollo@3.0.1 installed as well as @apollo/react-hooks@3.0.0. Removing @apollo/react-hooks and just relying on react-apollo fixed the invariant issue for me. Make sure that you aren’t using any mismatched versions in your lock file or package.json This is what someone said in a GitHub issue thread, which, was the … Read more

What is the difference between useQuery and useLazyQuery in Apollo graphQL?

When useQuery is called by the component, it triggers the query subsequently. But when useLazyQuery is called, it does not trigger the query subsequently, and instead return a function that can be used to trigger the query manually. It is explained on this page: https://www.apollographql.com/docs/react/data/queries/#manual-execution-with-uselazyquery When React mounts and renders a component that calls the … Read more

React Apollo – Make Multiple Queries

My preferred way is to use the compose functionality of the apollo client (docu). EDIT: If you have more than one query you should name them. So in your case, it could look like this: import React, {Component} from ‘react’ import queries from ‘./queries’ import { graphql, compose } from ‘react-apollo’; class Test extends Component … Read more

GraphQL dynamic query building

GraqhQL provides directives for this very purpose. Create a fragment to define common fields, use @include(if: Boolean) and @skip(if: Boolean) directives on that fragment to get dynamic fields. By dynamic fields we mean fields that are known at execution time. According to spec, it is best to avoid manual string interpolation to construct dynamic queries. … Read more