Return nested data in queries / mutations

prisma

#1

As per @agartha’s comment: Resolver function extend, resolvers are unable to return nested data (relations). But if resolvers are unable to return nested data, how, then, would we be able to do so via custom query/mutations?

For example, upon providing an affiliateLink, I want the clickAffiliateLink mutation to return a tokenString (generated in the AffiliateLinkClick model), as well as its related website title and url (located in the AffiliateLink model).

schema.graphql (incorrect syntax, no doubt):

# MUTATIONS
type Mutation {
	clickAffiliateLink(affiliateLink: String!): AffiliateLinkClickPayload!
}

# CUSTOM MUTATION PAYLOADS
type AffiliateLinkClickPayload {
	affiliateLink: AffiliateLinkClickConnectPayload
	tokenString: String!
}

type AffiliateLinkClickConnectPayload {
	connect: AffiliateLinkPayload
}

type AffiliateLinkPayload {
	title: String
	url: String
}

On the client-side, I want this to show, specifically the affiliateLink { title url } part:

mutation($affiliateLink: String!) {
	clickAffiliateLink(affiliateLink: $affiliateLink) {
		affiliateLink {
			title
			url
		}
		tokenString
	}
}

#2

The forum post you refer to is about Graphcool Framework.
You can absolutely return anything you want in GraphQL resolvers, for example using Prisma binding.

I recommend reading this forum post:

And this blog post:


#3

Thank you, @nilan!

One last question I wasn’t able to find the answer to in your blog post: How should the schema.graphql look like for a nested query resolver?

type Query {
	customQueryName(tokenString: String!): CustomQueryPayload!
}

type CustomQueryPayload {
	regularField: String! # ← This refers to the same model for which the query is performed.
	nestedField: ? # ← How can I make this refer to the other model? 
}

I would like to achieve this outcome:

query($tokenString: String!) {
	customQueryName(tokenString: $tokenString) {
		nestedField {
			title
			url
		}
		regularField
	}
}

The resolver looks like this:

const resolver = async (parent, args, ctx, info) => {
	const { tokenString } = args
	return await ctx.db.query.customQueryName({ where: { tokenString } }, `{ nestedField { title url } }`)
}

#4

Made it work! No idea how, thought… this whole thing is a bit over my head :flushed: