diff --git a/website/pages/execution.mdx b/website/pages/execution.mdx index 9b97c6e67f..2810ed183a 100644 --- a/website/pages/execution.mdx +++ b/website/pages/execution.mdx @@ -40,10 +40,14 @@ export function execute( type MaybePromise = Promise | T; -type ExecutionResult = { - data: Object; - errors?: GraphQLError[]; -}; +interface ExecutionResult< + TData = ObjMap, + TExtensions = ObjMap, +> { + errors?: ReadonlyArray; + data?: TData | null; + extensions?: TExtensions; +} ``` Implements the "Evaluating requests" section of the GraphQL specification. @@ -56,3 +60,25 @@ a GraphQLError will be thrown immediately explaining the invalid input. `ExecutionResult` represents the result of execution. `data` is the result of executing the query, `errors` is null if no errors occurred, and is a non-empty array if an error occurred. + +### executeSync + +```ts +export function executeSync( + schema: GraphQLSchema, + documentAST: Document, + rootValue?: mixed, + contextValue?: mixed, + variableValues?: { [key: string]: mixed }, + operationName?: string, +): ExecutionResult; + +type ExecutionResult = { + data: Object; + errors?: GraphQLError[]; +}; +``` + +This is a short-hand method that will call `execute` and when the response can +be returned synchronously it will be returned, when a `Promise` is returned this +method will throw an error. diff --git a/website/pages/graphql.mdx b/website/pages/graphql.mdx index 624aeeb10b..e6936f279c 100644 --- a/website/pages/graphql.mdx +++ b/website/pages/graphql.mdx @@ -144,6 +144,15 @@ function graphql( variableValues?: { [key: string]: any }, operationName?: string, ): Promise; + +interface ExecutionResult< + TData = ObjMap, + TExtensions = ObjMap, +> { + errors?: ReadonlyArray; + data?: TData | null; + extensions?: TExtensions; +} ``` The `graphql` function lexes, parses, validates and executes a GraphQL request. diff --git a/website/pages/type.mdx b/website/pages/type.mdx index 0eb05d3302..4ab3d7d1a2 100644 --- a/website/pages/type.mdx +++ b/website/pages/type.mdx @@ -188,16 +188,20 @@ const MyAppSchema = new GraphQLSchema({ ### GraphQLScalarType ```ts -class GraphQLScalarType { - constructor(config: GraphQLScalarTypeConfig); +class GraphQLScalarType { + constructor(config: GraphQLScalarTypeConfig); } -type GraphQLScalarTypeConfig = { +type GraphQLScalarTypeConfig = { name: string; description?: string; - serialize: (value: mixed) => InternalType; - parseValue?: (value: mixed) => InternalType; - parseLiteral?: (valueAST: Value) => InternalType; + specifiedByURL?: Maybe; + serialize: (outputValue: unknown) => ExternalType; + parseValue?: (inputValue: unknown) => InternalType; + parseLiteral?: ( + valueAST: Value, + variables?: Maybe>, + ) => InternalType; }; ``` @@ -210,19 +214,30 @@ functions used to ensure validity. ```js const OddType = new GraphQLScalarType({ name: 'Odd', - serialize: oddValue, - parseValue: oddValue, + // Can be used to link to a specification + // for this scalar, for instance the JSON + // specification. + specifiedByURL: '', + description: + '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.' + serialize: (outputValue) => { + // This function gets called for response-data, the application returns data + // for a property and in the schema we see that this value has the "Odd" type. + return typeof outputValue === 'number' && outputValue % 2 === 1 ? value : null; + }, + parseValue: (inputValue) => { + // This function gets called for input-data, i.e. variables being passed in + return typeof inputValue === 'number' && outputValue % 2 === 1 ? value : null; + }, parseLiteral(ast) { + // This function gets called when the value is passed in as a literal on the + // Executable GraphQL Document if (ast.kind === Kind.INT) { return oddValue(parseInt(ast.value, 10)); } return null; }, }); - -function oddValue(value) { - return value % 2 === 1 ? value : null; -} ``` ### GraphQLObjectType