Code Monkey home page Code Monkey logo

Comments (3)

Trellian avatar Trellian commented on September 3, 2024 3

Hi @nasushkov , do you perhaps have an example of how to access the models defined by build(), inside of a mutation? I don't see them available in the context of the mutation. The example given in the docs just returns a static result

from objection-graphql.

nasushkov avatar nasushkov commented on September 3, 2024 1

Hi, if you want to add mutations to this library you have two options:

  1. You can use extendWithMutations to enhance the generated schema with mutations. Here you need to use plain old verbose GraphQL js for type definitions). Here is an example
  2. You can define the other schema with mutations and then merge schemas with graphql-tools (it works well with the recent version from Apollo)

from objection-graphql.

ryanvanderpol avatar ryanvanderpol commented on September 3, 2024

@Trellian a bit late to the punch here but I was playing around with this today and discovered you can call getType on the built schema and use what comes back in your mutation. It seems that build is immutable (although adding models to the builder is not, which is a bit confusing) so you can call build before creating your mutation schemas, call extendWithMutations next, and then call build again at the end.

Here's an example:

const schema = schema.model(Person);

const graphQlSchema = schema.build();

const updatePersonType = new GraphQLInputObjectType({
	name: 'PersonInputType',
	description: 'Schema for updating a Person',
	fields: () => ({
		id: {
			type: new GraphQLNonNull(GraphQLInt),
			description: 'Unique ID',
		},
		name: {
			type: new GraphQLNonNull(GraphQLString),
			description: 'Name',
		},
	}),
});

const mutationType = new GraphQLObjectType({
	name: 'RootMutationType',
	description: 'Domain API actions',
	fields: () => ({
		updatePerson: {
			description: 'Updates a person',
			type: graphQlSchema.getType('Persons'),
			args: {
				input: { type: new GraphQLNonNull(updatePersonType) },
			},
			resolve: async (root, userData) => {
                const { id, name } = userData.input;

                return await Person.query().findById(id).patch({ name }).returning('*');
			},
		},
	}),
});

const finalSchema = schema.extendWithMutation(mutationType).build();

from objection-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.