Ich bin ziemlich neu zu GraphQL und ich versuche, ein Problem zu lösen, das mein Manager mir präsentiert hat.Erstellen einer verschachtelten Struktur in graphql
Ich habe die folgende Datenstruktur mir zur Verfügung gestellt, über eine 3rd-Party-API (die ich null Kontrolle haben):
[
{
"id": 19,
"date": "2016-10-24T13:59:19",
"date_gmt": "2016-10-24T12:59:19",
"slug: "data",
"provider": {
"name": "data",
"logo": "data",
"destination_url": "data",
"coupon_label": "data",
"coupon_text": "data",
"coupon_code": "data",
"coupon_url": "data",
}
}
]
Ich brauche es in ein GraphQL Schema zu drehen, die wie folgt aussieht query:
{
provider(slug: "slug") {
id
date
slug
name
logo
url
coupon {
label
text
code
url
}
}
}
ich habe es geschafft, unten mit dem Code sortieren die meisten davon aus, aber ich kann nicht herausfinden, wie sich die Coupon-Knoten in einer Gruppe zu.
Ich rate, dass dies ein weiterer benutzerdefinierter Typ sein muss? Wenn dies der Fall ist, scheint dies ineffizient zu sein, da ein Gutschein niemals außerhalb des Anbietertyps verwendet wird. Daher wollte ich wissen, ob es eine "Best Practice" -Methode gibt, die mir nicht bekannt ist.
import { GraphQLObjectType, GraphQLInt, GraphQLString } from 'graphql'
const ProviderType = new GraphQLObjectType({
name: 'Provider',
fields:() => ({
id: {
type: GraphQLInt,
description: 'The primary key for the provider'
},
slug: {
type: GraphQLString,
description: 'A unique string for the provider'
},
status: {
type: GraphQLString,
description: 'The the published status of the provider'
},
name: {
type: GraphQLString,
description: 'The name of the provider',
resolve (parent) { return parent.provider.name }
},
logo: {
type: GraphQLString,
description: 'The full url of the provider logo',
resolve (parent) { return parent.provider.logo }
},
url: {
type: GraphQLString,
description: 'The full url of the provider',
resolve (parent) { return parent.provider.destination_url }
},
})
})
export default ProviderType
Update:
Ich habe den Code wie folgt aktualisiert, aber es funktioniert immer noch nicht, so meine Annahme falsch gewesen sein muss (oder ich implementiert es falsch)
const ProviderType = new GraphQLObjectType({
name: 'Provider',
fields:() => ({
id: {
type: GraphQLInt,
description: 'The primary key for the provider'
},
slug: {
type: GraphQLString,
description: 'A unique string for the provider'
},
status: {
type: GraphQLString,
description: 'The the published status of the provider'
},
name: {
type: GraphQLString,
description: 'The name of the provider',
resolve (parent) { return parent.provider.name }
},
logo: {
type: GraphQLString,
description: 'The full url of the provider logo',
resolve (parent) { return parent.provider.logo }
},
url: {
type: GraphQLString,
description: 'The full url of the provider',
resolve (parent) { return parent.provider.destination_url }
},
coupon: {
type: CouponType,
description: 'The coupon information for the provider'
}
})
})
const CouponType = new GraphQLObjectType({
name: 'Coupon',
fields:() => ({
label: {
type: GraphQLString,
description: 'The label for the coupon',
resolve (parent) { return parent.provider.coupon_label }
},
text: {
type: GraphQLString,
description: 'The text for the coupon',
resolve (parent) { return parent.provider.coupon_text }
},
code: {
type: GraphQLString,
description: 'The code for the coupon',
resolve (parent) { return parent.provider.coupon_code }
},
url: {
type: GraphQLString,
description: 'The url for the coupon',
resolve (parent) { return parent.provider.coupon_url }
}
})
})
Sie benötigen einen Resolver in Ihrem Coupon-Feld in Ihrem Anbietertyp zurückkehren würden, da es ein verschachteltes/Unterfeld – vbranden