Skip to content
This repository has been archived by the owner on Feb 25, 2024. It is now read-only.

Commit

Permalink
Added graphql-codegen to viz
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Jul 22, 2021
1 parent 5f088b2 commit f409b22
Show file tree
Hide file tree
Showing 13 changed files with 2,448 additions and 120 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

*.generated.*
1 change: 1 addition & 0 deletions .graphqlconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
schema: https://registry-prod.graphcdn.app
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"graphql.vscode-graphql"
]
}
15 changes: 15 additions & 0 deletions codegen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
schema:
- https://registry-prod.graphcdn.app
documents:
- 'src/graphql/**/*.graphql'
generates:
graphql/schemaTypes.generated.ts:
- typescript
graphql/:
preset: near-operation-file
presetConfig:
baseTypesPath: 'schemaTypes.generated.ts'
extension: '.generated.ts'
plugins:
- typescript-operations
- typed-document-node
18 changes: 14 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
"react-scripts": "4.0.3",
"web-vitals": "^1.0.1",
"web-worker": "^1.0.0",
"xstate": "^4.23.0"
"xstate": "^4.23.0",
"@graphql-typed-document-node/core": "^3.1.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"start": "concurrently \"npm:start:app\" \"npm:graphql:codegen\"",
"start:app": "react-scripts start",
"build": "graphql-codegen && react-scripts build",
"graphql:codegen": "graphql-codegen --watch",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
Expand All @@ -57,6 +60,13 @@
"@types/react": "^17.0.6",
"@types/react-dom": "^17.0.5",
"sass": "^1.33.0",
"typescript": "^4.3.5"
"typescript": "^4.3.5",
"concurrently": "^6.2.0",
"@graphql-codegen/cli": "^1.21.5",
"@graphql-codegen/near-operation-file-preset": "^1.18.1",
"@graphql-codegen/typed-document-node": "^1.18.6",
"@graphql-codegen/typescript": "^1.22.1",
"@graphql-codegen/typescript-operations": "^1.18.0",
"graphql": "^15.5.0"
}
}
28 changes: 16 additions & 12 deletions src/clientMachine.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ import {
import {
ActorRefFrom,
assign,
createMachine,
EventFrom,
forwardTo,
MachineOptions,
send,
spawn,
} from 'xstate';
import { spawn } from 'xstate';
import { send } from 'xstate';
import { createMachine } from 'xstate';
import { createModel } from 'xstate/lib/model';
import { cacheCodeChangesMachine } from './cacheCodeChangesMachine';
import { confirmBeforeLeavingService } from './confirmLeavingService';
import { CreateSourceFileDocument } from './graphql/CreateSourceFile.generated';
import { UpdateSourceFileDocument } from './graphql/UpdateSourceFile.generated';
import { notifMachine, notifModel } from './notificationMachine';
import { CreateSourceQuery, UpdateSourceQuery } from './types';
import { gQuery, updateQueryParamsWithoutReload } from './utils';

const clientModel = createModel(
Expand Down Expand Up @@ -70,19 +71,22 @@ const clientOptions: Partial<
services: {
saveMachines: async (ctx, e) => {
if (e.type !== 'SAVE') return;
return gQuery<CreateSourceQuery>(
`mutation {createSourceFile(text: ${JSON.stringify(
e.rawSource,
)}) {id}}`,
return gQuery(
CreateSourceFileDocument,
{
text: e.rawSource,
},
ctx.client.auth.session()?.access_token!,
).then((data) => data.data?.createSourceFile);
},
updateMachines: async (ctx, e) => {
if (e.type !== 'UPDATE') return;
return gQuery<UpdateSourceQuery>(
`mutation {updateSourceFile(id: ${JSON.stringify(
e.id,
)}, text: ${JSON.stringify(e.rawSource)}) {id}}`,
return gQuery(
UpdateSourceFileDocument,
{
id: e.id,
text: e.rawSource,
},
ctx.client.auth.session()?.access_token!,
).then((data) => data.data?.updateSourceFile);
},
Expand Down
5 changes: 5 additions & 0 deletions src/graphql/CreateSourceFile.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation CreateSourceFile($text: String!) {
createSourceFile(text: $text) {
id
}
}
11 changes: 11 additions & 0 deletions src/graphql/GetSourceFile.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
query GetSourceFile($id: String!) {
getSourceFile(id: $id) {
id
updatedAt
owner {
id
avatarUrl
displayName
}
}
}
5 changes: 5 additions & 0 deletions src/graphql/UpdateSourceFile.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation UpdateSourceFile($id: String!, $text: String!) {
updateSourceFile(id: $id, text: $text) {
id
}
}
20 changes: 8 additions & 12 deletions src/sourceMachine.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {
ActorRefFrom,
assign,
ContextFrom,
createMachine,
DoneInvokeEvent,
send,
spawn,
ContextFrom,
} from 'xstate';
import { createModel } from 'xstate/lib/model';
import { GetSourceFileDocument } from './graphql/GetSourceFile.generated';
import { localCache } from './localCache';
import { notifMachine } from './notificationMachine';
import { GetSourceFile } from './types';
import { gQuery, updateQueryParamsWithoutReload } from './utils';

type SourceProvider = 'gist' | 'registry';
Expand Down Expand Up @@ -153,9 +153,7 @@ export const sourceMachine = createMachine<typeof sourceModel>(
isSourceIDAvailable: (ctx) => !!ctx.sourceID,
},
services: {
loadSourceContent: (
ctx,
): Promise<{ text: string; updatedAt?: string }> => {
loadSourceContent: (ctx) => {
switch (ctx.sourceProvider) {
case 'gist':
return fetch('https://api.github.com/gists/' + ctx.sourceID)
Expand All @@ -176,13 +174,11 @@ export const sourceMachine = createMachine<typeof sourceModel>(
);
});
case 'registry':
return gQuery<GetSourceFile>(
`query {getSourceFile(id: ${JSON.stringify(
ctx.sourceID,
)}) {id,text,updatedAt}}`,
).then((data) => {
if (data.data?.getSourceFile) {
return data.data.getSourceFile;
return gQuery(GetSourceFileDocument, {
id: ctx.sourceID,
}).then((res) => {
if (res.data?.getSourceFile) {
return Promise.resolve(res.data.getSourceFile);
}
return Promise.reject(
new NotFoundError('Source not found in Registry'),
Expand Down
20 changes: 0 additions & 20 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ import { Model } from 'xstate/lib/model.types';

export type AnyStateMachine = StateMachine<any, any, any>;

export type CreateSourceQuery = {
createSourceFile: {
id: string;
};
};

export type UpdateSourceQuery = {
updateSourceFile: {
id: string;
};
};

export type GetSourceFile = {
getSourceFile?: {
id: string;
text: string;
updatedAt: string;
};
};

export type StateFrom<T> = T extends StateMachine<
infer TContext,
any,
Expand Down
12 changes: 8 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TypedDocumentNode } from '@graphql-typed-document-node/core';
import * as React from 'react';
import type {
ActionObject,
Expand All @@ -6,6 +7,7 @@ import type {
TransitionDefinition,
} from 'xstate';
import { AnyState, AnyStateMachine } from './types';
import { print } from 'graphql';

export function createRequiredContext<T>(displayName: string) {
const context = React.createContext<T | null>(null);
Expand Down Expand Up @@ -104,18 +106,20 @@ export const updateQueryParamsWithoutReload = (
window.history.pushState({ path: newURL.href }, '', newURL.href);
};

export const gQuery = <Result>(
query: string,
export const gQuery = <Data, Variables>(
query: TypedDocumentNode<Data, Variables>,
variables: Variables,
accessToken?: string,
): Promise<{ data?: Result }> =>
): Promise<{ data?: Data }> =>
fetch(process.env.REACT_APP_GRAPHQL_API_URL, {
method: 'POST',
headers: {
'content-type': 'application/json',
...(accessToken && { authorization: 'Bearer ' + accessToken }),
},
body: JSON.stringify({
query,
query: print(query),
variables,
}),
}).then((resp) => resp.json());

Expand Down
Loading

0 comments on commit f409b22

Please sign in to comment.