Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
aor-graphql-client
Advanced tools
A GraphQL client for admin-on-rest built with Apollo
A version of the admin-on-rest
demo using this client is available at https://marmelab.com/admin-on-rest-graphql-demo.
The source code for this demo is available at https://github.com/marmelab/admin-on-rest-graphql-demo.
This is a very low level library which is not meant to be used directly unless you really want full control or are building a custom GraphQL client.
It provides the foundations for other packages such as:
This library is meant to be used with Apollo on the client side but you're free to use any graphql server.
Install with:
npm install --save aor-graphql-client
or
yarn add aor-graphql-client
// in App.js
import React, { Component } from 'react';
import buildApolloClient from 'aor-graphql-client';
import { Admin, Resource } from 'admin-on-rest';
import { Delete } from 'admin-on-rest/lib/mui';
import { PostCreate, PostEdit, PostList } from '../components/admin/posts';
const client = new ApolloClient();
class App extends Component {
constructor() {
super();
this.state = { restClient: null };
}
componentDidMount() {
buildApolloClient()
.then(restClient => this.setState({ restClient }));
}
render() {
const { restClient } = this.state;
if (!restClient) {
return <div>Loading</div>;
}
return (
<Admin restClient={restClient}>
<Resource name="Post" list={PostList} edit={PostEdit} create={PostCreate} remove={Delete} />
</Admin>
);
}
}
export default App;
You can specify the client options by calling buildApolloClient
like this:
import { createNetworkInterface } from 'react-apollo';
buildApolloClient({
clientOptions: {
networkInterface: createNetworkInterface({
uri: 'http://api.myproduct.com/graphql',
}),
},
});
You can pass any options supported by the ApolloClient contructor with the addition of uri
which can be specified so that we create the network interface for you.
You can also supply your own ApolloClient instance directly with:
buildApolloClient({ client: myClient });
Instead of running an IntrospectionQuery you can also provide the IntrospectionQuery result directly. This speeds up the initial rendering of the Admin
component as it no longer has to wait for the introspection query request to resolve.
import { __schema as schema } from './schema';
const introspectionOptions = {
schema
};
buildApolloClient({
introspection: introspectionOptions
});
The ./schema
file is a schema.json
in ./scr
retrieved with get-graphql-schema --json <graphql_endpoint>
.
Note: Importing the
schema.json
file will significantly increase the bundle size.
For the client to know how to map Admin-on-rest request to apollo queries and mutations, you must provide a queryBuilder
option. The queryBuilder
is a factory function which will be called with the introspection query result.
The introspection result is an object with 3 properties:
types
: an array of all the GraphQL types discovered on your endpointqueries
: an array of all the GraphQL queries and mutations discovered on your endpointresources
: an array of objects with a type
property, which is the GraphQL type for this resource, and a property for each Admin-on-rest fetch verb for which we found a matching query or mutationFor example:
{
types: [
{
name: 'Post',
kind: 'OBJECT',
fields: [
{ name: 'id', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'ID' } } },
{ name: 'title', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'String' } } },
...
]
},
...
],
queries: [
{
name: 'createPost',
args: [
{ name: 'title', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'String' } } }
],
type : { kind: 'OBJECT', name: 'Category' }
},
...
],
resources: [
{
type: {
name: 'Post',
kind: 'OBJECT',
fields: [
{ name: 'id', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'ID' } } },
{ name: 'title', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'String' } } },
...
]
},
GET_LIST: {
name: 'createPost',
args: [
{ name: 'title', type: { kind: 'NON_NULL', ofType: { kind: 'SCALAR', name: 'String' } } }
],
type : { kind: 'OBJECT', name: 'Category' }
},
...
}
]
}
The queryBuilder
function must return a function which will be called with the same parameters as the Admin-on-rest client but must return an object matching the options
of the ApolloClient query method with an additional parseResponse
function.
This parseResponse
function will be called with an ApolloQueryResult and must returns the data expected by Admin-on-rest.
For example:
import buildFieldList from './buildFieldList';
const queryBuilder = introspectionResults => (aorFetchType, resourceName, params) => {
const resource = introspectionResults.resource.find(r => r.type.name === resourceName);
switch (aorFetchType) {
case 'GET_ONE':
return {
query: gql`query ${resource[aorFetchType].name}($id: ID) {
data: ${resource[aorFetchType].name}(id: $id) {
${buildFieldList(introspectionResults, resource, aorFetchType)}
}
}`,
variables: params, // params = { id: ... }
parseResponse: response => response.data,
}
break;
// ... other types handled here
}
}
buildApolloClient({ queryBuilder });
When I create or edit a resource, the list or edit page does not refresh its data
admin-on-rest
maintain its own cache of resources data but, by default, so does the Apollo client. For every queries, we inject a default fetchPolicy
set to network-only
so that the Apollo client always refetch the data when requested.
Do not override this fetchPolicy
.
Run the tests with this command:
make test
FAQs
A GraphQL client for admin-on-rest
The npm package aor-graphql-client receives a total of 4 weekly downloads. As such, aor-graphql-client popularity was classified as not popular.
We found that aor-graphql-client demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.