@teambit/graphql 0.0.568 → 0.0.572
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/create-remote-schemas/create-remote-schemas.ts +58 -0
- package/create-remote-schemas/index.ts +1 -0
- package/graphql-provider/graphql-provider.tsx +12 -0
- package/graphql-provider/index.ts +1 -0
- package/graphql.preview.runtime.tsx +7 -0
- package/graphql.ui.runtime.tsx +103 -0
- package/package-tar/teambit-graphql-0.0.572.tgz +0 -0
- package/package.json +10 -25
- package/render-lifecycle.tsx +91 -0
- package/types/asset.d.ts +29 -0
- package/types/style.d.ts +42 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import fetch from 'node-fetch';
|
|
2
|
+
import { setContext } from 'apollo-link-context';
|
|
3
|
+
import { HttpLink } from 'apollo-link-http';
|
|
4
|
+
import { makeRemoteExecutableSchema, introspectSchema } from 'apollo-server';
|
|
5
|
+
import { WebSocketLink } from 'apollo-link-ws';
|
|
6
|
+
import { split } from 'apollo-link';
|
|
7
|
+
import { getMainDefinition } from 'apollo-utilities';
|
|
8
|
+
import { SubscriptionClient } from 'subscriptions-transport-ws';
|
|
9
|
+
import ws from 'ws';
|
|
10
|
+
import { GraphQLServer } from '../graphql-server';
|
|
11
|
+
|
|
12
|
+
async function getRemoteSchema({ uri, subscriptionsUri }) {
|
|
13
|
+
// @ts-ignore
|
|
14
|
+
const http = new HttpLink({ uri, fetch });
|
|
15
|
+
const httpLink = setContext((request, previousContext) => {
|
|
16
|
+
return {
|
|
17
|
+
headers: previousContext?.graphqlContext?.headers,
|
|
18
|
+
};
|
|
19
|
+
}).concat(http);
|
|
20
|
+
|
|
21
|
+
if (!subscriptionsUri) {
|
|
22
|
+
return makeRemoteExecutableSchema({
|
|
23
|
+
schema: await introspectSchema(httpLink),
|
|
24
|
+
link: httpLink,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Create WebSocket link with custom client
|
|
29
|
+
const client = new SubscriptionClient(subscriptionsUri, { reconnect: true }, ws);
|
|
30
|
+
const wsLink = new WebSocketLink(client);
|
|
31
|
+
|
|
32
|
+
// Using the ability to split links, we can send data to each link
|
|
33
|
+
// depending on what kind of operation is being sent
|
|
34
|
+
const link = split(
|
|
35
|
+
(operation) => {
|
|
36
|
+
const definition = getMainDefinition(operation.query);
|
|
37
|
+
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription';
|
|
38
|
+
},
|
|
39
|
+
wsLink,
|
|
40
|
+
httpLink
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
return makeRemoteExecutableSchema({
|
|
44
|
+
schema: await introspectSchema(httpLink),
|
|
45
|
+
link,
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function createRemoteSchemas(servers: GraphQLServer[]) {
|
|
50
|
+
const schemasP = servers.map(async (server) => {
|
|
51
|
+
return getRemoteSchema({
|
|
52
|
+
uri: server.uri,
|
|
53
|
+
subscriptionsUri: server.subscriptionsUri,
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return Promise.all(schemasP);
|
|
58
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createRemoteSchemas } from './create-remote-schemas';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { ApolloProvider } from '@apollo/client';
|
|
3
|
+
import type { GraphQLClient } from '../graphql.ui.runtime';
|
|
4
|
+
|
|
5
|
+
export type GraphQLProviderProps = {
|
|
6
|
+
client: GraphQLClient<any>;
|
|
7
|
+
children: ReactNode;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export function GraphQLProvider({ client, children }: GraphQLProviderProps) {
|
|
11
|
+
return <ApolloProvider client={client}>{children}</ApolloProvider>;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { GraphQLProvider } from './graphql-provider';
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { UIRuntime } from '@teambit/ui';
|
|
3
|
+
|
|
4
|
+
import { InMemoryCache, ApolloClient, ApolloLink, HttpLink, createHttpLink } from '@apollo/client';
|
|
5
|
+
import type { NormalizedCacheObject } from '@apollo/client';
|
|
6
|
+
import { WebSocketLink } from '@apollo/client/link/ws';
|
|
7
|
+
import { onError } from '@apollo/client/link/error';
|
|
8
|
+
|
|
9
|
+
import crossFetch from 'cross-fetch';
|
|
10
|
+
|
|
11
|
+
import { createSplitLink } from './create-link';
|
|
12
|
+
import { GraphQLProvider } from './graphql-provider';
|
|
13
|
+
import { GraphqlAspect } from './graphql.aspect';
|
|
14
|
+
import { GraphqlRenderLifecycle } from './render-lifecycle';
|
|
15
|
+
import { logError } from './logging';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Type of gql client.
|
|
19
|
+
* Used to abstract Apollo client, so consumers could import the type from graphql.ui, and not have to depend on @apollo/client directly
|
|
20
|
+
* */
|
|
21
|
+
export type GraphQLClient<T> = ApolloClient<T>;
|
|
22
|
+
|
|
23
|
+
type ClientOptions = {
|
|
24
|
+
/** Preset in-memory cache with state (e.g. continue state from SSR) */
|
|
25
|
+
state?: NormalizedCacheObject;
|
|
26
|
+
/** endpoint for websocket connections */
|
|
27
|
+
subscriptionUri?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export class GraphqlUI {
|
|
31
|
+
createClient(uri: string, { state, subscriptionUri }: ClientOptions = {}) {
|
|
32
|
+
const client = new ApolloClient({
|
|
33
|
+
link: this.createLink(uri, { subscriptionUri }),
|
|
34
|
+
cache: this.createCache({ state }),
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
return client;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
createSsrClient({ serverUrl, headers }: { serverUrl: string; headers: any }) {
|
|
41
|
+
const link = ApolloLink.from([
|
|
42
|
+
onError(logError),
|
|
43
|
+
createHttpLink({
|
|
44
|
+
credentials: 'include',
|
|
45
|
+
uri: serverUrl,
|
|
46
|
+
headers,
|
|
47
|
+
fetch: crossFetch,
|
|
48
|
+
}),
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
const client = new ApolloClient({
|
|
52
|
+
ssrMode: true,
|
|
53
|
+
link,
|
|
54
|
+
cache: this.createCache(),
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
return client;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
private createCache({ state }: { state?: NormalizedCacheObject } = {}) {
|
|
61
|
+
const cache = new InMemoryCache();
|
|
62
|
+
|
|
63
|
+
if (state) cache.restore(state);
|
|
64
|
+
|
|
65
|
+
return cache;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
private createLink(uri: string, { subscriptionUri }: { subscriptionUri?: string } = {}) {
|
|
69
|
+
const httpLink = new HttpLink({ credentials: 'include', uri });
|
|
70
|
+
const subsLink = subscriptionUri
|
|
71
|
+
? new WebSocketLink({
|
|
72
|
+
uri: subscriptionUri,
|
|
73
|
+
options: { reconnect: true },
|
|
74
|
+
})
|
|
75
|
+
: undefined;
|
|
76
|
+
|
|
77
|
+
const hybridLink = subsLink ? createSplitLink(httpLink, subsLink) : httpLink;
|
|
78
|
+
const errorLogger = onError(logError);
|
|
79
|
+
|
|
80
|
+
return ApolloLink.from([errorLogger, hybridLink]);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* get the graphQL provider
|
|
85
|
+
*/
|
|
86
|
+
getProvider = ({ client, children }: { client: GraphQLClient<any>; children: ReactNode }) => {
|
|
87
|
+
return <GraphQLProvider client={client}>{children}</GraphQLProvider>;
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
renderHooks = new GraphqlRenderLifecycle(this);
|
|
91
|
+
|
|
92
|
+
static runtime = UIRuntime;
|
|
93
|
+
static dependencies = [];
|
|
94
|
+
static slots = [];
|
|
95
|
+
|
|
96
|
+
static async provider() {
|
|
97
|
+
const graphqlUI = new GraphqlUI();
|
|
98
|
+
|
|
99
|
+
return graphqlUI;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
GraphqlAspect.addRuntime(GraphqlUI);
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/graphql",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.572",
|
|
4
4
|
"homepage": "https://bit.dev/teambit/harmony/graphql",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.harmony",
|
|
8
8
|
"name": "graphql",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.572"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
12
|
"@teambit/harmony": "0.2.11",
|
|
@@ -32,12 +32,12 @@
|
|
|
32
32
|
"bufferutil": "4.0.3",
|
|
33
33
|
"@babel/runtime": "7.12.18",
|
|
34
34
|
"core-js": "^3.0.0",
|
|
35
|
-
"@teambit/cli": "0.0.
|
|
36
|
-
"@teambit/logger": "0.0.
|
|
37
|
-
"@teambit/toolbox.network.get-port": "0.0.
|
|
38
|
-
"@teambit/preview": "0.0.
|
|
39
|
-
"@teambit/ui": "0.0.
|
|
40
|
-
"@teambit/ui-foundation.ui.is-browser": "0.0.
|
|
35
|
+
"@teambit/cli": "0.0.395",
|
|
36
|
+
"@teambit/logger": "0.0.483",
|
|
37
|
+
"@teambit/toolbox.network.get-port": "0.0.99",
|
|
38
|
+
"@teambit/preview": "0.0.572",
|
|
39
|
+
"@teambit/ui": "0.0.572",
|
|
40
|
+
"@teambit/ui-foundation.ui.is-browser": "0.0.472"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/cors": "2.8.10",
|
|
@@ -54,7 +54,7 @@
|
|
|
54
54
|
"peerDependencies": {
|
|
55
55
|
"@apollo/client": "^3.0.0",
|
|
56
56
|
"graphql": "14.7.0",
|
|
57
|
-
"@teambit/legacy": "1.0.
|
|
57
|
+
"@teambit/legacy": "1.0.181",
|
|
58
58
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
59
59
|
"react": "^16.8.0 || ^17.0.0"
|
|
60
60
|
},
|
|
@@ -84,27 +84,12 @@
|
|
|
84
84
|
"react": "-"
|
|
85
85
|
},
|
|
86
86
|
"peerDependencies": {
|
|
87
|
-
"@teambit/legacy": "1.0.
|
|
87
|
+
"@teambit/legacy": "1.0.181",
|
|
88
88
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
89
89
|
"react": "^16.8.0 || ^17.0.0"
|
|
90
90
|
}
|
|
91
91
|
}
|
|
92
92
|
},
|
|
93
|
-
"files": [
|
|
94
|
-
"dist",
|
|
95
|
-
"!dist/tsconfig.tsbuildinfo",
|
|
96
|
-
"**/*.md",
|
|
97
|
-
"**/*.mdx",
|
|
98
|
-
"**/*.js",
|
|
99
|
-
"**/*.json",
|
|
100
|
-
"**/*.sass",
|
|
101
|
-
"**/*.scss",
|
|
102
|
-
"**/*.less",
|
|
103
|
-
"**/*.css",
|
|
104
|
-
"**/*.css",
|
|
105
|
-
"**/*.jpeg",
|
|
106
|
-
"**/*.gif"
|
|
107
|
-
],
|
|
108
93
|
"private": false,
|
|
109
94
|
"engines": {
|
|
110
95
|
"node": ">=12.22.0"
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
import { getDataFromTree } from '@apollo/client/react/ssr';
|
|
3
|
+
import type { NormalizedCacheObject } from '@apollo/client';
|
|
4
|
+
import pick from 'lodash.pick';
|
|
5
|
+
|
|
6
|
+
import { isBrowser } from '@teambit/ui-foundation.ui.is-browser';
|
|
7
|
+
import type { BrowserData, RenderLifecycle } from '@teambit/ui';
|
|
8
|
+
|
|
9
|
+
import type { GraphqlUI, GraphQLClient } from './graphql.ui.runtime';
|
|
10
|
+
import { GraphQLProvider } from './graphql-provider';
|
|
11
|
+
|
|
12
|
+
type RenderContext = {
|
|
13
|
+
client: GraphQLClient<any>;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const ALLOWED_HEADERS = ['cookie'];
|
|
17
|
+
|
|
18
|
+
export class GraphqlRenderLifecycle implements RenderLifecycle<RenderContext, { state?: NormalizedCacheObject }> {
|
|
19
|
+
constructor(private graphqlUI: GraphqlUI) {}
|
|
20
|
+
|
|
21
|
+
serverInit = ({ browser, server }: { browser?: BrowserData; server?: { port: number } } = {}) => {
|
|
22
|
+
if (!browser) return undefined;
|
|
23
|
+
|
|
24
|
+
const port = server?.port || 3000;
|
|
25
|
+
const serverUrl = `http://localhost:${port}/graphql`;
|
|
26
|
+
|
|
27
|
+
const client = this.graphqlUI.createSsrClient({
|
|
28
|
+
serverUrl,
|
|
29
|
+
headers: pick(browser.connection.headers, ALLOWED_HEADERS),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const ctx: RenderContext = { client };
|
|
33
|
+
return ctx;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Eagerly and recursively execute all gql queries in the app.
|
|
38
|
+
* Data will be available in gqlClient.extract()
|
|
39
|
+
*/
|
|
40
|
+
onBeforeRender = async (ctx: RenderContext, app: ReactNode) => {
|
|
41
|
+
await getDataFromTree(app);
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
serialize = (ctx?: RenderContext) => {
|
|
45
|
+
const client = ctx?.client;
|
|
46
|
+
if (!client) return undefined;
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
json: JSON.stringify(client.extract()),
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
deserialize = (raw?: string) => {
|
|
54
|
+
if (!raw) return { state: undefined };
|
|
55
|
+
let state: NormalizedCacheObject | undefined;
|
|
56
|
+
try {
|
|
57
|
+
state = JSON.parse(raw);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
// eslint-disable-next-line no-console
|
|
60
|
+
console.error('[GraphQL] failed deserializing state from DOM', e);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return { state };
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
browserInit = ({ state }: { state?: NormalizedCacheObject } = {}) => {
|
|
67
|
+
const { location } = window;
|
|
68
|
+
const isInsecure = location.protocol === 'http:';
|
|
69
|
+
const wsUrl = `${isInsecure ? 'ws:' : 'wss:'}//${location.host}/subscriptions`;
|
|
70
|
+
|
|
71
|
+
const client = this.graphqlUI.createClient('/graphql', { state, subscriptionUri: wsUrl });
|
|
72
|
+
|
|
73
|
+
return { client };
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
private BrowserGqlProvider = ({ renderCtx, children }: { renderCtx?: RenderContext; children: ReactNode }) => {
|
|
77
|
+
if (!renderCtx?.client) throw new TypeError('GQL client is not initialized, make sure `.browserInit()` executes');
|
|
78
|
+
|
|
79
|
+
return <this.graphqlUI.getProvider client={renderCtx?.client}>{children}</this.graphqlUI.getProvider>;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
reactContext = isBrowser ? this.BrowserGqlProvider : ServerGqlProvider;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function ServerGqlProvider({ renderCtx, children }: { renderCtx?: RenderContext; children: ReactNode }) {
|
|
86
|
+
if (!renderCtx?.client)
|
|
87
|
+
throw new TypeError('GQL client has not been initialized during SSR, make sure `.serverInit()` executes');
|
|
88
|
+
|
|
89
|
+
const { client } = renderCtx;
|
|
90
|
+
return <GraphQLProvider client={client}>{children}</GraphQLProvider>;
|
|
91
|
+
}
|
package/types/asset.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
declare module '*.png' {
|
|
2
|
+
const value: any;
|
|
3
|
+
export = value;
|
|
4
|
+
}
|
|
5
|
+
declare module '*.svg' {
|
|
6
|
+
import type { FunctionComponent, SVGProps } from 'react';
|
|
7
|
+
|
|
8
|
+
export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
|
|
9
|
+
const src: string;
|
|
10
|
+
export default src;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// @TODO Gilad
|
|
14
|
+
declare module '*.jpg' {
|
|
15
|
+
const value: any;
|
|
16
|
+
export = value;
|
|
17
|
+
}
|
|
18
|
+
declare module '*.jpeg' {
|
|
19
|
+
const value: any;
|
|
20
|
+
export = value;
|
|
21
|
+
}
|
|
22
|
+
declare module '*.gif' {
|
|
23
|
+
const value: any;
|
|
24
|
+
export = value;
|
|
25
|
+
}
|
|
26
|
+
declare module '*.bmp' {
|
|
27
|
+
const value: any;
|
|
28
|
+
export = value;
|
|
29
|
+
}
|
package/types/style.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
declare module '*.module.css' {
|
|
2
|
+
const classes: { readonly [key: string]: string };
|
|
3
|
+
export default classes;
|
|
4
|
+
}
|
|
5
|
+
declare module '*.module.scss' {
|
|
6
|
+
const classes: { readonly [key: string]: string };
|
|
7
|
+
export default classes;
|
|
8
|
+
}
|
|
9
|
+
declare module '*.module.sass' {
|
|
10
|
+
const classes: { readonly [key: string]: string };
|
|
11
|
+
export default classes;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare module '*.module.less' {
|
|
15
|
+
const classes: { readonly [key: string]: string };
|
|
16
|
+
export default classes;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
declare module '*.less' {
|
|
20
|
+
const classes: { readonly [key: string]: string };
|
|
21
|
+
export default classes;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
declare module '*.css' {
|
|
25
|
+
const classes: { readonly [key: string]: string };
|
|
26
|
+
export default classes;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
declare module '*.sass' {
|
|
30
|
+
const classes: { readonly [key: string]: string };
|
|
31
|
+
export default classes;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
declare module '*.scss' {
|
|
35
|
+
const classes: { readonly [key: string]: string };
|
|
36
|
+
export default classes;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
declare module '*.mdx' {
|
|
40
|
+
const component: any;
|
|
41
|
+
export default component;
|
|
42
|
+
}
|