Skip to content

Commit 84a49f7

Browse files
authored
Solve some low hanging fruit in the documentation (#4279)
Ultimately I was trying this out to see whether we can tweak the docs easily, it made me realize that our docs are tailored to general GraphQL rather than how do we use this library. It made me come up with a few suggestions - We should have a toggle on code examples to switch between `buildSchema` and programatically creating the schema with i.e. `GraphQLObjectType` - Our documentation starts with a tutorial, this ultimately feels like a mistake, we should lead with an explanation of what GraphQL.JS is and what it aims to do, clearly outlining the goals of this project - We should line out use-cases for building on this library and best practices of how to go to production Resolves #2941 Resolves #2567
1 parent 16b3d01 commit 84a49f7

File tree

3 files changed

+66
-16
lines changed

3 files changed

+66
-16
lines changed

website/pages/execution.mdx

+30-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ export function execute(
4040

4141
type MaybePromise<T> = Promise<T> | T;
4242

43-
type ExecutionResult = {
44-
data: Object;
45-
errors?: GraphQLError[];
46-
};
43+
interface ExecutionResult<
44+
TData = ObjMap<unknown>,
45+
TExtensions = ObjMap<unknown>,
46+
> {
47+
errors?: ReadonlyArray<GraphQLError>;
48+
data?: TData | null;
49+
extensions?: TExtensions;
50+
}
4751
```
4852

4953
Implements the "Evaluating requests" section of the GraphQL specification.
@@ -56,3 +60,25 @@ a GraphQLError will be thrown immediately explaining the invalid input.
5660
`ExecutionResult` represents the result of execution. `data` is the result of
5761
executing the query, `errors` is null if no errors occurred, and is a
5862
non-empty array if an error occurred.
63+
64+
### executeSync
65+
66+
```ts
67+
export function executeSync(
68+
schema: GraphQLSchema,
69+
documentAST: Document,
70+
rootValue?: mixed,
71+
contextValue?: mixed,
72+
variableValues?: { [key: string]: mixed },
73+
operationName?: string,
74+
): ExecutionResult;
75+
76+
type ExecutionResult = {
77+
data: Object;
78+
errors?: GraphQLError[];
79+
};
80+
```
81+
82+
This is a short-hand method that will call `execute` and when the response can
83+
be returned synchronously it will be returned, when a `Promise` is returned this
84+
method will throw an error.

website/pages/graphql.mdx

+9
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ function graphql(
144144
variableValues?: { [key: string]: any },
145145
operationName?: string,
146146
): Promise<GraphQLResult>;
147+
148+
interface ExecutionResult<
149+
TData = ObjMap<unknown>,
150+
TExtensions = ObjMap<unknown>,
151+
> {
152+
errors?: ReadonlyArray<GraphQLError>;
153+
data?: TData | null;
154+
extensions?: TExtensions;
155+
}
147156
```
148157

149158
The `graphql` function lexes, parses, validates and executes a GraphQL request.

website/pages/type.mdx

+27-12
Original file line numberDiff line numberDiff line change
@@ -188,16 +188,20 @@ const MyAppSchema = new GraphQLSchema({
188188
### GraphQLScalarType
189189

190190
```ts
191-
class GraphQLScalarType<InternalType> {
192-
constructor(config: GraphQLScalarTypeConfig<InternalType>);
191+
class GraphQLScalarType<InternalType, ExternalType> {
192+
constructor(config: GraphQLScalarTypeConfig<InternalType, ExternalType>);
193193
}
194194

195-
type GraphQLScalarTypeConfig<InternalType> = {
195+
type GraphQLScalarTypeConfig<InternalType, ExternalType> = {
196196
name: string;
197197
description?: string;
198-
serialize: (value: mixed) => InternalType;
199-
parseValue?: (value: mixed) => InternalType;
200-
parseLiteral?: (valueAST: Value) => InternalType;
198+
specifiedByURL?: Maybe<string>;
199+
serialize: (outputValue: unknown) => ExternalType;
200+
parseValue?: (inputValue: unknown) => InternalType;
201+
parseLiteral?: (
202+
valueAST: Value,
203+
variables?: Maybe<Record<string, unknown>>,
204+
) => InternalType;
201205
};
202206
```
203207

@@ -210,19 +214,30 @@ functions used to ensure validity.
210214
```js
211215
const OddType = new GraphQLScalarType({
212216
name: 'Odd',
213-
serialize: oddValue,
214-
parseValue: oddValue,
217+
// Can be used to link to a specification
218+
// for this scalar, for instance the JSON
219+
// specification.
220+
specifiedByURL: '',
221+
description:
222+
'This custom scalar will only return a value if the passed in value is an odd integer, when it's not it will return null.'
223+
serialize: (outputValue) => {
224+
// This function gets called for response-data, the application returns data
225+
// for a property and in the schema we see that this value has the "Odd" type.
226+
return typeof outputValue === 'number' && outputValue % 2 === 1 ? value : null;
227+
},
228+
parseValue: (inputValue) => {
229+
// This function gets called for input-data, i.e. variables being passed in
230+
return typeof inputValue === 'number' && outputValue % 2 === 1 ? value : null;
231+
},
215232
parseLiteral(ast) {
233+
// This function gets called when the value is passed in as a literal on the
234+
// Executable GraphQL Document
216235
if (ast.kind === Kind.INT) {
217236
return oddValue(parseInt(ast.value, 10));
218237
}
219238
return null;
220239
},
221240
});
222-
223-
function oddValue(value) {
224-
return value % 2 === 1 ? value : null;
225-
}
226241
```
227242
228243
### GraphQLObjectType

0 commit comments

Comments
 (0)