-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
pipinet
committed
Sep 30, 2024
1 parent
35dac47
commit e947bcb
Showing
16 changed files
with
592 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
- TODO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
{ | ||
"name": "@dorami/apollo", | ||
"version": "0.0.0", | ||
"main": "./src/index.ts", | ||
"module": "./src/index.ts", | ||
"types": "./src/index.ts", | ||
"publishConfig": { | ||
"main": "./index.mjs", | ||
"module": "./index.mjs", | ||
"types": "./index.d.mts", | ||
"exports": { | ||
".": { | ||
"types": "./index.d.mts", | ||
"import": "./index.mjs" | ||
} | ||
}, | ||
"directory": "dist", | ||
"linkDirectory": false, | ||
"access": "public", | ||
"registry": "https://registry.npmjs.org/" | ||
}, | ||
"scripts": { | ||
"build": "NODE_ENV=production INPUT_DIR=./ OUTPUT_DIR=dist/ pnpm run build:package", | ||
"build:package": "pnpm run build:prebuild && tsup && pnpm run build:postbuild", | ||
"build:prebuild": "tsx ./scripts/prebuild.ts", | ||
"build:postbuild": "tsx ./scripts/postbuild.ts", | ||
"dev:link": "pnpm link --global && npm link" | ||
}, | ||
"dependencies": { | ||
"@apollo/client": "^3.11.8", | ||
"@vue/apollo-composable": "^4.2.1" | ||
}, | ||
"devDependencies": { | ||
"tsup": "^8.2.4", | ||
"tsx": "^4.16.2" | ||
}, | ||
"author": "Qwlabs", | ||
"homepage": "https://github.com/qwlabs/dorami", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/qwlabs/dorami.git", | ||
"directory": "packages/apollo" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/qwlabs/dorami/issues" | ||
}, | ||
"engines": { | ||
"node": ">=20.17.0", | ||
"pnpm": ">=9" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as path from 'node:path'; | ||
import * as fs from 'fs-extra'; | ||
import { clearPackageJson, resolvePath } from '../../../scripts/build-helper'; | ||
|
||
const { __dirname, __workspace, OUTPUT_DIR } = resolvePath(import.meta.url); | ||
|
||
fs.copySync(path.resolve(__dirname, '../package.json'), `${OUTPUT_DIR}/package.json`); | ||
fs.copySync(path.resolve(__dirname, '../README.md'), `${OUTPUT_DIR}/README.md`); | ||
fs.copySync(path.resolve(__workspace, './LICENSE.md'), `${OUTPUT_DIR}/LICENSE.md`); | ||
|
||
clearPackageJson(path.resolve(__dirname, `../${OUTPUT_DIR}/package.json`)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import * as path from 'node:path'; | ||
import { removeBuild, resolvePath, updatePackageJson } from '../../../scripts/build-helper'; | ||
|
||
removeBuild(import.meta.url); | ||
updatePackageJson(path.resolve(resolvePath(import.meta.url).__dirname, '../package.json')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import type { ApolloCache, NormalizedCacheObject, Operation } from '@apollo/client/core'; | ||
import type { GraphqlApiClientConfig } from './models'; | ||
import { ApolloClient, ApolloLink, createHttpLink, InMemoryCache, split } from '@apollo/client/core'; | ||
import { onError } from '@apollo/client/link/error'; | ||
import { getMainDefinition } from '@apollo/client/utilities'; | ||
import { DEFAULT_OPTIONS } from './models'; | ||
|
||
const loadHeaders = (config: GraphqlApiClientConfig): Record<string, any> => { | ||
let headers: Record<string, any> = {}; | ||
headers['Content-Type'] = 'application/json;charset=UTF-8'; | ||
headers['Accept-Language'] = 'zh-CN'; | ||
if (config.headerAugmentor) { | ||
headers = config.headerAugmentor(headers); | ||
} | ||
return headers; | ||
}; | ||
|
||
const createAHttpLink = (config: GraphqlApiClientConfig): ApolloLink => { | ||
const normalHttpLink = createHttpLink({ | ||
uri: (params: Operation) => { | ||
return `${config.url}?op=${params.operationName}`; | ||
}, | ||
}); | ||
const batchHttpLink = createHttpLink({ | ||
uri: config.url, | ||
}); | ||
return split(() => config.batchEnabled, batchHttpLink, normalHttpLink); | ||
}; | ||
|
||
const createWSLink = (config: GraphqlApiClientConfig): ApolloLink => { | ||
return ApolloLink.from([]); | ||
}; | ||
|
||
const createGraphqlLink = (config: GraphqlApiClientConfig): ApolloLink => { | ||
const httpLink = createAHttpLink(config); | ||
const wsLink = createWSLink(config); | ||
return split( | ||
({ query }) => { | ||
const definition = getMainDefinition(query); | ||
return definition.kind === 'OperationDefinition' && definition.operation === 'subscription'; | ||
}, | ||
wsLink, | ||
httpLink | ||
); | ||
}; | ||
|
||
const createErrorLink = (config: GraphqlApiClientConfig): ApolloLink => { | ||
return onError(({ graphQLErrors, networkError }) => { | ||
if (graphQLErrors != null && graphQLErrors.length > 0) { | ||
graphQLErrors.forEach((graphQLError) => { | ||
const handler = config.graphQLErrorHandlers.find((handler) => handler.match(graphQLError)); | ||
handler?.handle(graphQLError); | ||
}); | ||
} | ||
if (networkError) { | ||
const handler = config.networkErrorHandlers.find((handler) => handler.match(networkError)); | ||
handler?.handle(networkError); | ||
} | ||
}); | ||
}; | ||
|
||
const createMiddleware = (config: GraphqlApiClientConfig): ApolloLink => { | ||
return new ApolloLink((operation, forward) => { | ||
operation.setContext({ | ||
headers: loadHeaders(config), | ||
}); | ||
return forward(operation); | ||
}); | ||
}; | ||
|
||
const createLink = (config: GraphqlApiClientConfig): ApolloLink => { | ||
const middleware = createMiddleware(config); | ||
const graphqlLink = createGraphqlLink(config); | ||
const errorLink = createErrorLink(config); | ||
return ApolloLink.from([errorLink, middleware, graphqlLink]); | ||
}; | ||
|
||
export const createClient = (config: GraphqlApiClientConfig): { client: ApolloClient<any>; cache: ApolloCache<NormalizedCacheObject> } => { | ||
const link = createLink(config); | ||
const cache = new InMemoryCache({ | ||
resultCaching: true, | ||
}); | ||
const client = new ApolloClient({ | ||
link, | ||
cache, | ||
ssrMode: config.ssrMode, | ||
queryDeduplication: true, | ||
defaultOptions: config.defaultOptions ?? DEFAULT_OPTIONS, | ||
devtools: { | ||
enabled: config.connectToDevTools, | ||
} | ||
}); | ||
return { client, cache }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export interface RelyEdge<N> { | ||
node: N; | ||
} | ||
|
||
export interface RelyConnection<N> { | ||
edges: RelyEdge<N>[]; | ||
} | ||
|
||
export const extractRelyData = <N, R>(connection: unknown, mapping?: (node: N) => R): R[] => { | ||
return ((connection as RelyConnection<N>)?.edges ?? []).map((edge: RelyEdge<N>) => { | ||
return mapping ? mapping(edge.node) : (edge.node as unknown as R); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './graphql-client'; | ||
export * from './graphql-helpers'; | ||
export * from './models'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { FetchResult, Observable } from '@apollo/client'; | ||
import type { DefaultOptions } from '@apollo/client/core/ApolloClient'; | ||
import type { NetworkError } from '@apollo/client/errors'; | ||
import type { GraphQLFormattedError } from 'graphql/error/GraphQLError'; | ||
|
||
export type RequestHeaderAugmentor = (headers: Record<string, any>) => Record<string, any>; | ||
|
||
export interface ErrorMatchHandler { | ||
match: (error: Error) => boolean; | ||
|
||
handle: (error: Error) => Observable<FetchResult> | void; | ||
} | ||
|
||
export interface NetworkErrorMatchHandler extends ErrorMatchHandler { | ||
match: (error: NetworkError) => boolean; | ||
|
||
handle: (error: NetworkError) => Observable<FetchResult> | void; | ||
} | ||
|
||
export interface GraphQLErrorMatchHandler extends ErrorMatchHandler { | ||
match: (error: GraphQLFormattedError) => boolean; | ||
|
||
handle: (error: GraphQLFormattedError) => Observable<FetchResult> | void; | ||
} | ||
|
||
export interface GraphqlApiClientConfig { | ||
ssrMode: boolean; | ||
connectToDevTools: boolean; | ||
url: string; | ||
subscriptionUrl?: string; | ||
batchEnabled: boolean; | ||
headerAugmentor?: RequestHeaderAugmentor; | ||
networkErrorHandlers: NetworkErrorMatchHandler[]; | ||
graphQLErrorHandlers: GraphQLErrorMatchHandler[]; | ||
defaultOptions?: DefaultOptions; | ||
} | ||
|
||
export const DEFAULT_OPTIONS: DefaultOptions = { | ||
query: { | ||
notifyOnNetworkStatusChange: true, | ||
fetchPolicy: 'no-cache', | ||
}, | ||
mutate: { | ||
fetchPolicy: 'no-cache', | ||
keepRootFields: true, | ||
}, | ||
watchQuery: { | ||
notifyOnNetworkStatusChange: true, | ||
fetchPolicy: 'no-cache', | ||
nextFetchPolicy: 'no-cache', | ||
returnPartialData: true, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"baseUrl": "." | ||
}, | ||
"exclude": [ | ||
"node_modules", | ||
"dist" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { defineConfig } from 'tsup'; | ||
|
||
export default defineConfig({ | ||
entry: ['src/index.ts'], | ||
format: ['esm'], | ||
dts: true, | ||
splitting: false, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"baseUrl": ".", | ||
}, | ||
"exclude": ["node_modules", "dist"] | ||
"compilerOptions": { | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"baseUrl": ".", | ||
}, | ||
"exclude": ["node_modules", "dist"] | ||
} |
Oops, something went wrong.