4.16.15 • Published 27 days ago

ra-data-graphql-simple v4.16.15

Weekly downloads
1,512
License
MIT
Repository
github
Last release
27 days ago

ra-data-graphql-simple

A GraphQL data provider for react-admin built with Apollo and tailored to target a simple GraphQL implementation.

This is an example implementation to show how to build a graphql adapter using ra-data-graphql.

Installation

Install with:

npm install --save graphql ra-data-graphql-simple

or

yarn add graphql ra-data-graphql-simple

Usage

The ra-data-graphql-simple package exposes a single function, which is a constructor for a dataProvider based on a GraphQL endpoint. When executed, this function calls the GraphQL endpoint, running an introspection query. It uses the result of this query (the GraphQL schema) to automatically configure the dataProvider accordingly.

// in App.js
import React from 'react';
import { Component } from 'react';
import buildGraphQLProvider from 'ra-data-graphql-simple';
import { Admin, Resource } from 'react-admin';

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

const App = () => {

    const [dataProvider, setDataProvider] = React.useState(null);
    React.useEffect(() => {
        buildGraphQLProvider({ clientOptions: { uri: 'http://localhost:4000' } })
            .then(graphQlDataProvider => setDataProvider(() => graphQlDataProvider));
    }, []);

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

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

export default App;

Note: the parser will generate additional .id properties for relation based types. These properties should be used as sources for reference based fields and inputs like ReferenceField: <ReferenceField label="Author Name" source="author.id" reference="User">.

Expected GraphQL Schema

The ra-data-graphql-simple function works against GraphQL servers that respect a certain GraphQL grammar. For instance, to handle all the actions on a Post resource, the GraphQL endpoint should support the following schema:

type Query {
  Post(id: ID!): Post
  allPosts(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: PostFilter): [Post]
  _allPostsMeta(page: Int, perPage: Int, sortField: String, sortOrder: String, filter: PostFilter): ListMetadata
}

type Mutation {
  createPost(
    title: String!
    views: Int!
    user_id: ID!
  ): Post
  updatePost(
    id: ID!
    title: String!
    views: Int!
    user_id: ID!
  ): Post
  deletePost(id: ID!): Post
}

type Post {
    id: ID!
    title: String!
    views: Int!
    user_id: ID!
    User: User
    Comments: [Comment]
}

input PostFilter {
    q: String
    id: ID
    title: String
    views: Int
    views_lt: Int
    views_lte: Int
    views_gt: Int
    views_gte: Int
    user_id: ID
}

type ListMetadata {
    count: Int!
}

scalar Date

This is the grammar used e.g. by marmelab/json-graphql-server, a client-side GraphQL server used for test purposes.

Options

Customize the Apollo client

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

buildGraphQLProvider({ clientOptions: { uri: 'http://localhost:4000', ...otherApolloOptions } });

Or supply your client directly with:

buildGraphQLProvider({ 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 buildGraphQLProvider, { buildQuery } from 'ra-data-graphql-simple';

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 buildGraphQLProvider({ 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

Your GraphQL backend may 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 if your GraphQL backend support query batching.

Contributing

Run the tests with this command:

make test
4.16.15

27 days ago

5.0.0-alpha.1

1 month ago

5.0.0-alpha.0

1 month ago

4.16.12

2 months ago

4.16.11

3 months ago

4.16.10

3 months ago

4.16.9

3 months ago

4.16.8

3 months ago

4.16.7

3 months ago

4.16.6

4 months ago

4.16.5

4 months ago

4.16.0

6 months ago

4.16.1

5 months ago

4.16.2

5 months ago

4.15.5

6 months ago

4.15.0

7 months ago

4.15.2

6 months ago

4.14.3

7 months ago

4.14.0

8 months ago

4.13.4

8 months ago

4.13.0

9 months ago

4.12.0

10 months ago

4.12.1

10 months ago

4.12.2

10 months ago

4.11.4

10 months ago

4.11.0

11 months ago

4.11.3

11 months ago

4.9.4

1 year ago

4.10.1

1 year ago

4.8.4

1 year ago

4.8.0

1 year ago

4.8.3

1 year ago

4.4.3

2 years ago

4.6.0

1 year ago

4.3.0

2 years ago

4.2.5

2 years ago

4.2.1

2 years ago

4.2.0

2 years ago

4.1.4

2 years ago

4.1.3

2 years ago

4.0.3

2 years ago

4.0.2

2 years ago

4.0.0-RC.1

2 years ago

4.0.0-rc.0

2 years ago

4.0.0-beta.2

2 years ago

3.19.11

2 years ago

3.19.5

2 years ago

3.19.8

2 years ago

4.0.0-alpha.1

2 years ago

4.0.0-alpha.2

2 years ago

4.0.0-beta.1.0

2 years ago

3.19.10

2 years ago

3.19.1

2 years ago

3.19.0

3 years ago

4.0.0-alpha.0

3 years ago

3.18.3

3 years ago

3.13.2

3 years ago

3.11.4

3 years ago

3.10.2

3 years ago

3.10.1

3 years ago

3.9.5

4 years ago

3.9.2

4 years ago

3.9.1

4 years ago

3.9.0

4 years ago

3.9.0-beta.3

4 years ago

3.9.0-beta.2

4 years ago

3.9.0-beta.1

4 years ago

3.8.3

4 years ago

3.8.0

4 years ago

3.6.1

4 years ago

3.5.5

4 years ago

3.5.3

4 years ago

3.4.3

4 years ago

3.4.2

4 years ago

3.2.2

4 years ago

3.1.4

4 years ago

3.1.2

4 years ago

3.1.0

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

2.9.7

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.5

5 years ago

2.9.3

5 years ago

2.9.0

5 years ago

2.8.6

5 years ago

2.8.5

5 years ago

2.8.4

5 years ago

2.8.2

5 years ago

2.7.1

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