ra-data-graphql-plan4event
A GraphQL data provider for react-admin built with Apollo and tailored to target the GraphCool service. With a minor modification to keep Id naming consistent.
Installation
Install with:
npm install --save graphql ra-data-graphql-plan4event
or
yarn add graphql ra-data-graphql-plan4event
Usage
This example assumes a Post
type is defined in the graphcool schema.
// in App.js;;; ; const client = ; { super; thisstate = dataProvider: null ; } { ; } { const dataProvider = thisstate; if !dataProvider return <div>Loading</div>; return <Admin dataProvider=dataProvider> <Resource name="Post" list=PostList edit=PostEdit create=PostCreate remove=Delete /> </Admin> ; } ;
And that's it, buildGraphcoolProvider
will create a default ApolloClient for you and run an introspection query on your graphcool endpoint, listing all potential resources.
This works with any GraphCool endpoint, or any GraphQL endpoint modeled after the GraphCool grammar:
Options
Customize the Apollo client
You can either supply the client options by calling buildGraphcoolProvider
like this:
;
Or supply your client directly with:
;
Overriding a specific query
The default behavior might not be optimized especially when dealing with references. You can override a specific query by wrapping the buildQuery
function:
// in src/dataProvider.js; const myBuildQuery = { const builtQuery = fetchType resource params; if resource === 'Command' && fetchType === 'GET_ONE' return // Use the default query variables and parseResponse ...builtQuery // Override the query query: gql` query Command($id: ID!) { data: Command(id: $id) { id reference customer { id firstName lastName } } }` ; return builtQuery;} buildQuery: myBuildQuery
Customize the introspection
These are the default options for introspection:
const introspectionOptions = include: // Either an array of types to include or a function which will be called for every type discovered through introspection exclude: // Either an array of types to exclude or a function which will be called for every type discovered through introspection // Including typesconst introspectionOptions = include: 'Post' 'Comment'; // Excluding typesconst introspectionOptions = exclude: 'CommandItem'; // Including types with a functionconst introspectionOptions = 'Post' 'Comment'; // Including types with a functionconst introspectionOptions = !'Post' 'Comment';
Note: exclude
and include
are mutualy exclusives and include
will take precendance.
Note: When using functions, the type
argument will be a type returned by the introspection query. Refer to the introspection documentation for more information.
Pass the introspection options to the buildApolloProvider
function:
;
DELETE_MANY
and UPDATE_MANY
Optimizations
Graphcool does not allow multiple deletions or updates in a single query. This provider simply makes multiple requests to handle those. This is obviously not ideal but can be alleviated by supplying your own ApolloClient
which could use the apollo-link-batch-http link. Indeed, Graphcool does support query batching.
Contributing
Run the tests with this command:
make test