npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ra-data-graphql-amplication

v0.0.14

Published

A GraphQL simple data provider for react-admin

Downloads

621

Readme

ra-data-graphql-amplication

A GraphQL data provider for react-admin built for GraphQL API generated with Amplication

Installation

Install with:

npm install --save graphql ra-data-graphql-amplication

or

yarn add graphql ra-data-graphql-amplication

Usage

The ra-data-graphql-amplication 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 graphqlDataProvider.ts
import buildGraphQLProvider from 'ra-data-graphql-amplication';
import { ApolloClient } from 'apollo-client';
import { createHttpLink } from 'apollo-link-http';
import { setContext } from 'apollo-link-context';
import { InMemoryCache } from 'apollo-cache-inmemory';

const httpLink = createHttpLink({
  //@todo: get the url from a configuration file
  uri: 'http://localhost:3000/graphql',
});

const authLink = setContext((_, { headers }) => {
  //@todo: get the authentication token from local storage
  const token = 'YWRtaW46YWRtaW4=';
  return {
    headers: {
      ...headers,
      authorization: token ? `Basic ${token}` : '',
    },
  };
});

const apolloClient = new ApolloClient({
  cache: new InMemoryCache(),
  link: authLink.concat(httpLink),
});

export default buildGraphQLProvider({
  client: apolloClient,
});
//in app.tsx
import React, { useEffect, useState } from 'react';
import { Admin, DataProvider, Resource } from 'react-admin';

import buildGraphQLProvider from './graphqlDataProvider';
import basicHttpAuthProvider from './auth-provider/ra-auth-basic-http';

import './App.css';
import Dashboard from './pages/Dashboard';

import { UserList } from './User/UserList';
import { UserEdit } from './User/UserEdit';
import { UserCreate } from './User/UserCreate';

function App() {
  const [dataProvider, setDataProvider] = useState<DataProvider | null>(null);
  useEffect(() => {
    buildGraphQLProvider
      .then((provider) => {
        setDataProvider(() => provider);
      })
      .catch((error: any) => {
        console.log(error);
      });
  }, []);
  if (!dataProvider) {
    return <div>Loading</div>;
  }
  return (
    <div className="App">
      <Admin
        title="My Admin"
        dataProvider={dataProvider}
        authProvider={basicHttpAuthProvider}
        dashboard={Dashboard}
      >
        <Resource
          name="User"
          list={UserList}
          edit={UserEdit}
          create={UserCreate}
        />
      </Admin>
    </div>
  );
}

export default App;

Expected GraphQL Schema

The ra-data-graphql-amplication function works against GraphQL servers that was generated with Amplication, or respects its grammar.

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 });

Credits

This provider was built on top of the source code of ra-data-graphql-simple

https://github.com/marmelab/react-admin/tree/master/packages/ra-data-graphql-simple