3.6.1 • Published 4 years ago

ra-data-graphcool v3.6.1

Weekly downloads
89
License
MIT
Repository
github
Last release
4 years ago

ra-data-graphcool

A GraphQL data provider for react-admin built with Apollo and tailored to target the GraphCool service.

Installation

Install with:

npm install --save graphql ra-data-graphcool

or

yarn add graphql ra-data-graphcool

Usage

This example assumes a Post type is defined in the graphcool schema.

// in App.js
import * as React from 'react';
import { Component } from 'react';
import buildGraphcoolProvider from 'ra-data-graphcool';
import { Admin, Resource, Delete } from 'react-admin';

import { PostCreate, PostEdit, PostList } from './posts';

class App extends Component {
    constructor() {
        super();
        this.state = { dataProvider: null };
    }
    componentDidMount() {
        buildGraphcoolProvider({ clientOptions: { uri: 'https://api.graph.cool/simple/v1/graphcool_id' }})
            .then(dataProvider => this.setState({ dataProvider }));
    }

    render() {
        const { dataProvider } = this.state;

        if (!dataProvider) {
            return <div>Loading</div>;
        }

        return (
            <Admin dataProvider={dataProvider}>
                <Resource name="Post" list={PostList} edit={PostEdit} create={PostCreate} remove={Delete} />
            </Admin>
        );
    }
}

export default App;

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:

GraphCool grammar

Options

Customize the Apollo client

You can either supply the client options by calling buildGraphcoolProvider like this:

buildGraphcoolProvider({ clientOptions: { uri: 'https://api.graph.cool/simple/v1/graphcool_id', ...otherApolloOptions } });

Or supply your client directly with:

buildGraphcoolProvider({ client: myClient });

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
import buildGraphcoolProvider, { buildQuery } from 'ra-data-graphcool';

const myBuildQuery = introspection => (fetchType, resource, params) => {
    const builtQuery = buildQuery(introspection)(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;
}

export default buildGraphcoolProvider({ 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 types
const introspectionOptions = {
    include: ['Post', 'Comment'],
};

// Excluding types
const introspectionOptions = {
    exclude: ['CommandItem'],
};

// Including types with a function
const introspectionOptions = {
    include: type => ['Post', 'Comment'].includes(type.name),
};

// Including types with a function
const introspectionOptions = {
    exclude: type => !['Post', 'Comment'].includes(type.name),
};

Note: exclude and include are mutually exclusives and include will take precedence.

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:

buildApolloProvider({ introspection: introspectionOptions });

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
3.6.1

4 years ago

3.6.0

4 years ago

3.5.5

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.4.0

4 years ago

3.2.2

4 years ago

3.1.4

4 years ago

3.0.2

4 years ago

3.0.1

4 years ago

3.0.0

4 years ago

3.0.0-beta.6

4 years ago

3.0.0-beta.3

5 years ago

2.9.8

5 years ago

3.0.0-beta.2

5 years ago

3.0.0-beta.1

5 years ago

3.0.0-alpha.4

5 years ago

3.0.0-alpha.3

5 years ago

2.9.6

5 years ago

3.0.0-alpha.1

5 years ago

3.0.0-alpha.0

5 years ago

2.9.3

5 years ago

2.8.4

5 years ago

2.8.2

5 years ago

2.6.0

5 years ago

2.5.3

5 years ago

2.5.2

5 years ago

2.5.1

5 years ago

2.5.0

5 years ago

2.4.4

5 years ago

2.4.3

5 years ago

2.4.2

5 years ago

2.4.1

6 years ago

2.4.0

6 years ago

2.4.0-0

6 years ago

2.3.4

6 years ago

2.3.3

6 years ago

2.3.2

6 years ago

2.3.1

6 years ago

2.3.0

6 years ago

2.2.4

6 years ago

2.2.3

6 years ago

2.2.2

6 years ago

2.2.1

6 years ago

2.2.0

6 years ago

2.1.5

6 years ago

2.1.4

6 years ago

2.1.3

6 years ago

2.1.2

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.4

6 years ago

2.0.3

6 years ago

2.0.2

6 years ago

2.0.0

6 years ago

2.0.0-RC4

6 years ago

2.0.0-RC3

6 years ago

2.0.0-RC2

6 years ago

2.0.0-RC1

6 years ago

2.0.0-beta4

6 years ago

2.0.0-beta3

6 years ago

2.0.0-beta2

6 years ago

2.0.0-beta1

6 years ago