graphql-map-fragment-types
v0.0.4
Published
Extract map of fragment types from an express based GraphQL app.
Downloads
3
Readme
GraphQL Map Fragment Types
Extract map of fragment types from an express based GraphQL app.
Solves issue presented on the Apollo Client documentation, explained in the following paragraph:
By default, Apollo Client doesn't require any knowledge of the GraphQL schema, which means it's very easy to set up and works with any server and supports even the largest schemas. However, as your usage of Apollo and GraphQL becomes more sophisticated, you may start using fragments on interfaces or unions. Here's an example of a query that uses fragments on an interface:
query { all_people { ... on Character { name } ... on Jedi { side } ... on Droid { model } } }
In the query above,
all_people
returns a result of typeCharacter[]
. BothJedi
andDroid
are possible concrete types ofCharacter
, but on the client there is no way to know that without having some information about the schema. By default, Apollo Client's cache will use a heuristic fragment matcher, which assumes that a fragment matched if the result included all the fields in its selection set, and didn't match when any field was missing. This works in most cases, but it also means that Apollo Client cannot check the server response for you, and it cannot tell you when you're manually writing an invalid data into the store usingupdate
,updateQuery
,writeQuery
, etc.
Usage
A sample express app that mounts it's graphQL middleware on the endpoint '/graph':
const app = require('./express')();
const graph = require('./graph'); // Your GraphQL middleware
app.use('/graphql', graph);
module.exports = app;
Sample usage:
const app = require('./app.js');
const mapFragmentTypes = require('graphql-map-fragment-types');
(async () => {
try {
const types = await mapFragmentTypes(app);
// Print the types into a JSON file which can then be imported by your client
} catch (error) {
// Handle error
console.error(error);
}
})();