-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
context.ts
51 lines (45 loc) · 1.3 KB
/
context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { GraphQLSchema } from "graphql"
import { SchemaComposer } from "graphql-compose"
import { createPageDependency } from "../redux/actions/add-page-dependency"
import { LocalNodeModel } from "./node-model"
import { defaultFieldResolver } from "./resolvers"
import { IGraphQLRunnerStats } from "../query/types"
import { IGatsbyResolverContext, IGraphQLSpanTracer } from "./type-definitions"
export default function withResolverContext<TSource, TArgs>({
schema,
schemaComposer,
context,
customContext,
nodeModel,
stats,
tracer,
}: {
schema: GraphQLSchema
schemaComposer: SchemaComposer<IGatsbyResolverContext<TSource, TArgs>> | null
context?: Record<string, any>
customContext?: Record<string, any>
nodeModel?: any
stats?: IGraphQLRunnerStats | null
tracer?: IGraphQLSpanTracer
}): IGatsbyResolverContext<TSource, TArgs> {
const nodeStore = require(`../db/nodes`)
if (!nodeModel) {
nodeModel = new LocalNodeModel({
nodeStore,
schema,
schemaComposer,
createPageDependency,
})
}
return {
...(context || {}),
...(customContext || {}),
defaultFieldResolver,
nodeModel: nodeModel.withContext({
path: context ? context.path : undefined,
}),
stats: stats || null,
tracer: tracer || null,
}
}
module.exports = withResolverContext