Add mutationField shorthand helper (#58)

* Add mutationField helper, closes #46
This commit is contained in:
Tim Griesser 2019-02-23 12:25:12 -05:00 committed by GitHub
parent 69f79583d7
commit 4e894c2081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 147 additions and 22 deletions

View File

@ -1,5 +1,10 @@
# Changelog
### 0.10.0
- Add an optional field level authorize #32, part of a more robust authorization story in #23
- Add mutationField abstraction, #46
### 0.9.17
- More for #55, type error on Promise/null resolve

View File

@ -26,3 +26,4 @@ Each public API is documented below, feel free to open a PR with more examples/c
- [args](api-args.md)
- [makeSchema](api-makeSchema.md)
- [extendType / extendInputType](api-extendType.md)
- [mutationField](api-mutationField.md)

32
docs/api-mutationField.md Normal file
View File

@ -0,0 +1,32 @@
---
id: api-mutationField
title: mutationField
sidebar_label: mutationField
---
Mutations are usually best split up, and are one of the most common use-cases for `extendType`. `mutationField` exists as a shorthand for this common case:
```ts
export const createUser = mutationField("createUser", {
type: SomeType,
resolve() {
// ...
},
});
```
as shorthand for:
```ts
export const createUser = extendType({
type: "Mutation",
definition(t) {
t.field('createUser', {
type: SomeType
resolve() {
// ...
}
})
}
})
```

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/

View File

@ -1801,6 +1801,11 @@ cross-spawn@^6.0.0, cross-spawn@^6.0.5:
shebang-command "^1.2.0"
which "^1.2.9"
cssom@0.3.x:
version "0.3.6"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.6.tgz#f85206cee04efa841f3c5982a74ba96ab20d65ad"
integrity sha512-DtUeseGk9/GBW0hl0vVPpU22iHL6YB5BUX7ml1hB+GMpo0NX5G4voX3kdWiMSEguFtcW3Vh3djqNF4aIe6ne0A==
"cssom@>= 0.3.2 < 0.4.0":
version "0.3.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.4.tgz#8cd52e8a3acfd68d3aed38ee0a640177d2f9d797"

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly
"""

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/

View File

@ -83,6 +83,9 @@ exports.Mutation = objectType({
args: {
repoFullName: RepoNameArg,
},
resolve() {
// todo
},
});
t.field("vote", {
type: "Entry",
@ -96,6 +99,9 @@ exports.Mutation = objectType({
description: "The type of vote - UP, DOWN, or CANCEL",
}),
},
resolve() {
// todo
},
});
t.field("submitComment", {
type: "Comment",
@ -106,13 +112,16 @@ exports.Mutation = objectType({
description: "The text content for the new comment",
}),
},
resolve() {
// todo
},
});
},
});
/**
* Example of using functions to mixin fields across types
* @type {(t: import('nexus').Types.ObjectDefinitionBlock<any>) => void}
* @type {(t: import('nexus').core.ObjectDefinitionBlock<any>) => void}
*/
const commonFields = (t) => {
t.int("id", { description: "The SQL ID of this entry" });

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly
@ -27,6 +27,11 @@ input InputType {
key: String!
}
type Mutation {
ok: Boolean!
someMutationField(id: ID!): Foo!
}
type Query {
bar: Bar!
extended: Bar!

View File

@ -9,6 +9,8 @@ import {
extendInputType,
intArg,
idArg,
mutationField,
mutationType,
} from "nexus";
export const testArgs1 = {
@ -19,6 +21,22 @@ export const testArgs2 = {
bar: idArg(),
};
export const Mutation = mutationType({
definition(t) {
t.boolean("ok", () => true);
},
});
export const SomeMutationField = mutationField("someMutationField", () => ({
type: Foo,
args: {
id: idArg({ required: true }),
},
resolve(root, args) {
return { name: `Test${args.id}`, ok: true };
},
}));
export const Bar = interfaceType({
name: "Bar",
description: "Bar description",

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/
@ -30,6 +30,7 @@ export interface NexusGenRootTypes {
name: string; // String!
ok: boolean; // Boolean!
}
Mutation: {};
Query: {};
TestObj: { // root type
a: NexusGenRootTypes['Bar']; // Bar!
@ -57,6 +58,10 @@ export interface NexusGenFieldTypes {
name: string; // String!
ok: boolean; // Boolean!
}
Mutation: { // field return type
ok: boolean; // Boolean!
someMutationField: NexusGenRootTypes['Foo']; // Foo!
}
Query: { // field return type
bar: NexusGenRootTypes['Bar']; // Bar!
extended: NexusGenRootTypes['Bar']; // Bar!
@ -84,6 +89,11 @@ export interface NexusGenArgTypes {
a?: NexusGenInputs['InputType'] | null; // InputType
}
}
Mutation: {
someMutationField: { // args
id: string; // ID!
}
}
Query: {
getNumberOrNull: { // args
a: number; // Int!
@ -109,7 +119,7 @@ export interface NexusGenAbstractResolveReturnTypes {
export interface NexusGenInheritedFields {}
export type NexusGenObjectNames = "Foo" | "Query" | "TestObj";
export type NexusGenObjectNames = "Foo" | "Mutation" | "Query" | "TestObj";
export type NexusGenInputNames = "InputType";

View File

@ -1112,6 +1112,11 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"
cssom@0.3.x:
version "0.3.6"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.6.tgz#f85206cee04efa841f3c5982a74ba96ab20d65ad"
integrity sha512-DtUeseGk9/GBW0hl0vVPpU22iHL6YB5BUX7ml1hB+GMpo0NX5G4voX3kdWiMSEguFtcW3Vh3djqNF4aIe6ne0A==
"cssom@>= 0.3.2 < 0.4.0":
version "0.3.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.4.tgz#8cd52e8a3acfd68d3aed38ee0a640177d2f9d797"

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly

View File

@ -1112,6 +1112,11 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"
cssom@0.3.x:
version "0.3.6"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.6.tgz#f85206cee04efa841f3c5982a74ba96ab20d65ad"
integrity sha512-DtUeseGk9/GBW0hl0vVPpU22iHL6YB5BUX7ml1hB+GMpo0NX5G4voX3kdWiMSEguFtcW3Vh3djqNF4aIe6ne0A==
"cssom@>= 0.3.2 < 0.4.0":
version "0.3.4"
resolved "https://registry.yarnpkg.com/cssom/-/cssom-0.3.4.tgz#8cd52e8a3acfd68d3aed38ee0a640177d2f9d797"

View File

@ -1,5 +1,5 @@
/**
* This file was automatically generated by Nexus 0.9.14
* This file was automatically generated by Nexus 0.9.17
* Do not make changes to this file directly
*/

View File

@ -1,10 +1,10 @@
import { Types } from "nexus";
import { core } from "nexus";
export function withTypeArguments(t: Types.OutputDefinitionBlock<any>) {
export function withTypeArguments(t: core.OutputDefinitionBlock<any>) {
t.list.field("typeArguments", { type: "Node", nullable: true });
}
export function hasTypeParameters(t: Types.OutputDefinitionBlock<any>) {
export function hasTypeParameters(t: core.OutputDefinitionBlock<any>) {
t.field("typeParameters", {
type: "TypeParameterDeclaration",
list: true,
@ -12,7 +12,7 @@ export function hasTypeParameters(t: Types.OutputDefinitionBlock<any>) {
});
}
export function signatureDeclarationBase(t: Types.OutputDefinitionBlock<any>) {
export function signatureDeclarationBase(t: core.OutputDefinitionBlock<any>) {
hasTypeParameters(t);
t.string("nameText", {
nullable: true,
@ -22,13 +22,13 @@ export function signatureDeclarationBase(t: Types.OutputDefinitionBlock<any>) {
t.field("type", { type: "Node", nullable: true });
}
export function functionLikeDeclaration(t: Types.ObjectDefinitionBlock<any>) {
export function functionLikeDeclaration(t: core.ObjectDefinitionBlock<any>) {
signatureDeclarationBase(t);
t.implements("MaybeOptional");
t.field("asteriskToken", { type: "Token", nullable: true });
t.field("exclamationToken", { type: "Token", nullable: true });
}
export function nodeType(t: Types.ObjectDefinitionBlock<any>) {
export function nodeType(t: core.ObjectDefinitionBlock<any>) {
t.implements("Node");
}

View File

@ -11,7 +11,12 @@ export const UNKNOWN_NODE = objectType({
export const Token = objectType({
name: "Token",
definition(t) {
t.field("kind", { type: "SyntaxKind" });
t.field("kind", {
type: "SyntaxKind",
resolve(root) {
return root.kind;
},
});
},
});

View File

@ -1,4 +1,4 @@
### This file was autogenerated by Nexus 0.9.14
### This file was autogenerated by Nexus 0.9.17
### Do not make changes to this file directly

View File

@ -16,6 +16,7 @@ export * from "./definitions/extendType";
export * from "./definitions/extendInputType";
export * from "./definitions/inputObjectType";
export * from "./definitions/interfaceType";
export * from "./definitions/mutationField";
export * from "./definitions/objectType";
export * from "./definitions/scalarType";
export * from "./definitions/unionType";

View File

@ -0,0 +1,17 @@
import { FieldOutConfig } from "../core";
import { extendType } from "./extendType";
export function mutationField<FieldName extends string>(
fieldName: FieldName,
config:
| FieldOutConfig<"Mutation", FieldName>
| (() => FieldOutConfig<"Mutation", FieldName>)
) {
return extendType({
type: "Mutation",
definition(t) {
const finalConfig = typeof config === "function" ? config() : config;
t.field(fieldName, finalConfig);
},
});
}

View File

@ -14,6 +14,7 @@ export { extendInputType } from "./definitions/extendInputType";
export { inputObjectType } from "./definitions/inputObjectType";
export { interfaceType } from "./definitions/interfaceType";
export { objectType, queryType, mutationType } from "./definitions/objectType";
export { mutationField } from "./definitions/mutationField";
export { scalarType, asNexusMethod } from "./definitions/scalarType";
export { unionType } from "./definitions/unionType";
export { nexusWrappedType } from "./definitions/wrapping";

View File

@ -209,3 +209,8 @@ export function firstDefined<T>(...args: Array<T | undefined>): T {
}
throw new Error("At least one of the values should be defined");
}
// eslint-disable-next-line no-redeclare
export function isPromise(value: any): value is PromiseLike<any> {
return Boolean(value && typeof value.then === "function");
}

View File

@ -19,7 +19,8 @@
"api-enumType",
"api-args",
"api-makeSchema",
"api-extendType"
"api-extendType",
"api-mutationField"
]
}
}