Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql data provider #202

Closed
wants to merge 13 commits into from
Prev Previous commit
Next Next commit
Update getList provider.
  • Loading branch information
yildirayunlu committed Mar 5, 2021
commit bc26bc20bd66a7f38ad51ef70d62ecd8cb8fc324
27 changes: 25 additions & 2 deletions example-graphql/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import React from "react";
import { useEffect } from "react";
import { useState } from "react";

import { Admin, Resource, AuthProvider, JsonGraphqlServer } from "readmin";
import {
Admin,
Resource,
AuthProvider,
JsonGraphqlServer,
createClient,
introspectionQuery,
} from "readmin";
import { PostList, PostCreate, PostEdit } from "./components/pages/post";

function App() {
Expand Down Expand Up @@ -32,10 +41,24 @@ function App() {
}),
};

const client = createClient("http:https://localhost:3000");

const [introspection, setIntrospection] = useState(null);

useEffect(() => {
introspectionQuery(client).then((response: any) =>
setIntrospection(response),
);
}, []);

if (!introspection) {
return <span>loading...</span>;
}

return (
<Admin
authProvider={authProvider}
dataProvider={JsonGraphqlServer("/api")}
dataProvider={JsonGraphqlServer(client, introspection)}
>
<Resource
name="Posts"
Expand Down
35 changes: 34 additions & 1 deletion example-graphql/src/components/pages/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
Form,
TextField,
Input,
useApiUrl,
InputNumber,
} from "readmin";

export const PostList = (props: any) => {
Expand Down Expand Up @@ -66,6 +66,17 @@ export const PostCreate = (props: any) => {
return (
<Create {...props}>
<Form wrapperCol={{ span: 14 }} layout="vertical">
<Form.Item
label="Id"
name="id"
rules={[
{
required: true,
},
]}
>
<InputNumber />
</Form.Item>
<Form.Item
label="Title"
name="title"
Expand All @@ -77,6 +88,28 @@ export const PostCreate = (props: any) => {
>
<Input />
</Form.Item>
<Form.Item
label="Views"
name="views"
rules={[
{
required: true,
},
]}
>
<InputNumber />
</Form.Item>
<Form.Item
label="UserId"
name="user_id"
rules={[
{
required: true,
},
]}
>
<InputNumber />
</Form.Item>
</Form>
</Create>
);
Expand Down
17 changes: 7 additions & 10 deletions src/contexts/data/IDataContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export interface GetListResponse<TData extends BaseRecord = BaseRecord> {
total: number;
}

export interface CreateResponse<TData extends BaseRecord = BaseRecord> {
data: TData;
export interface CreateResponse {
data: BaseRecord;
}

export interface UpdateResponse {
Expand All @@ -38,8 +38,8 @@ export interface UpdateManyResponse {
data: BaseRecord[];
}

export interface GetOneResponse<TData extends BaseRecord = BaseRecord> {
data: TData;
export interface GetOneResponse {
data: BaseRecord;
}

export interface GetManyResponse<TData extends BaseRecord = BaseRecord> {
Expand Down Expand Up @@ -74,11 +74,8 @@ export interface IDataContext {
resource: string,
id: Identifier,
fields?: string[],
) => Promise<GetOneResponse<TData>>;
create: <TData extends BaseRecord = BaseRecord>(
resource: string,
params: Params,
) => Promise<CreateResponse<TData>>;
) => Promise<GetOneResponse>;
create: (resource: string, params: Params) => Promise<CreateResponse>;
update: <TParams extends BaseRecord = BaseRecord>(
resource: string,
id: Identifier,
Expand All @@ -94,5 +91,5 @@ export interface IDataContext {
resource: string,
ids: Identifier[],
) => Promise<DeleteManyResponse>;
getApiUrl: () => string;
getApiUrl?: () => string;
}
8 changes: 8 additions & 0 deletions src/dataProviders/graphql/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { GraphQLClient } from "graphql-request";
import { RequestInit } from "graphql-request/dist/types.dom";

const createClient = (url: string, options?: RequestInit) => {
return new GraphQLClient(url, options);
};

export { createClient };
2 changes: 2 additions & 0 deletions src/dataProviders/graphql/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createClient } from "./client";
export { introspectionQuery } from "./introspection";
4 changes: 3 additions & 1 deletion src/dataProviders/graphql/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { getIntrospectionQuery, IntrospectionQuery } from "graphql";
import { GraphQLClient } from "graphql-request";
import gql from "graphql-tag";

export default async (client: GraphQLClient) => {
const introspectionQuery = async (client: GraphQLClient) => {
const introspectionRequest: Promise<IntrospectionQuery> = client.request(
gql`
${getIntrospectionQuery()}
Expand Down Expand Up @@ -33,3 +33,5 @@ export default async (client: GraphQLClient) => {
types,
};
};

export { introspectionQuery };
48 changes: 48 additions & 0 deletions src/dataProviders/graphql/utility.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as gqlBuilder from "gql-query-builder";
import { singular } from "pluralize";
import IQueryBuilderOptions from "gql-query-builder/build/IQueryBuilderOptions";
import { GraphQLClient } from "graphql-request";

type Actions =
| "getList"
| "getMany"
| "getOne"
| "create"
| "update"
| "updateMany"
| "deleteOne"
| "deleteMany"
| "metaData";

const createOperationName = (resourceName: string, action: Actions): string => {
switch (action) {
case "create":
case "update":
return `${action}${singular(resourceName)}`;
case "getList":
return `all${resourceName}`;
case "metaData":
return `_all${resourceName}Meta`;
}
return "";
};

const query = async (
client: GraphQLClient,
options: IQueryBuilderOptions | IQueryBuilderOptions[],
) => {
const qb = gqlBuilder.query(options);

return client.request(qb.query, qb.variables);
};

const mutation = async (
client: GraphQLClient,
options: IQueryBuilderOptions | IQueryBuilderOptions[],
) => {
const qb = gqlBuilder.mutation(options);

return client.request(qb.query, qb.variables);
};

export { createOperationName, query, mutation };
Loading