How to access data from grandparent in apollo graphql server? - graphql

I am trying to access data from grandparent in a grandchild resolver. How do I achieve this?
{
field1
field2 {
child1 {
grandchild1
}
}
}
I want to access field1 in grandchild1. How to go about that?

If you can directly pass field2 object as the parent arg of grandchild1.
const resolvers = {
Query : {
field2 : {return field2 obj}
},
Field2: {
child1 : {return field2 obj}
},
Child1: {
grandchild1 : {access field2 values return grandchild1}
}
}

Related

How to cast into a type in GraphQL

For example, through GitHub explorer one can retrieve different types of time line items for a pull request (in this example PULL_REQUEST_COMMIT and PULL_REQUEST_REVIEW):
{
repository(name: "react", owner: "facebook") {
pullRequests(last: 10) {
nodes {
number
timelineItems(last: 10, itemTypes: [PULL_REQUEST_COMMIT, PULL_REQUEST_REVIEW]) {
nodes {
__typename
}
}
}
}
}
}
How can I now access different fields of the types PullRequestEvent or PullRequestReviewEvent? In other words, is there a cast or an if-then-else in GraphQL?
nodes returns an array of PullRequestTimelineItems and a PullRequestTimelineItemsis a GraphQL union type. You can use the ...on notation to query for fields of a specific member in the union type:
{
repository(name: "react", owner: "facebook") {
pullRequests(last: 10) {
nodes {
number
timelineItems(last: 10, itemTypes: [PULL_REQUEST_COMMIT, PULL_REQUEST_REVIEW]) {
nodes {
...on PullRequestReview {
body
}
...on PullRequestCommit {
url
}
}
}
}
}
}
}

GQL scheme to accept multiple data structure for same key

I'm currently using GQL Modules in my app.
In the below data structure, content will have either object or array
var A = {
content: {
text: "Hello"
}
}
var B = {
content: {
banner: [{
text: "Hello"
}]
}
}
How do I make content to accept dynamic schema ?
Below is what I tired, but not working. Please help
type body {
content: TextContent | [Banner]
}
type Banner {
text: TextContent
}
type TextContent {
text: String
}
GraphQL requires that a field always resolves to either a single value or a list -- it cannot resolve to either. A field can however, return different types altogether at runtime using an abstract type (either a union or an interface). So you can restructure your schema like this:
type Body {
content: Content
}
union Content = TextContent | BannerContent
type TextContent {
text: String
}
type BannerContent {
banners: [Banner]
}
You would then query content using fragments:
query {
someField {
body {
content: {
...on TextContent {
text
}
...on BannerContent {
banners
}
}
}
}
}

How to get all Enum values in graphql

How to retrieve​ all values in enum type in graphql ?
Example:
enum TRUCKPE_NAME {
TATA_407
TATA_709
TATA_1106
ECHIER_1103
}
type Document {
truckType: TRUCKPE_NAME
}
I want to get all names inside above enum. something like
console.log(prisma.Documents().truckType())
// output
TATA_407
TATA_709
TATA_1106
ECHIER_1103
You can run an introspection query:
const { data: { __type: { enumValues } } } = await prisma.request(`
{
__type(name: "TRUCKPE_NAME") {
enumValues {
name
}
}
}
`)
const values = enumValues.map(({ name }) => name)
query {
__type(name:"GenderEnum"){
name
enumValues{
name
}
}
}
credits and more details:
https://medium.com/novvum/how-to-query-enums-with-graphql-using-introspection-daa048014700

Drupal GraphQL - Query Single Entity from URL

Is it possible to get an article(single entity) using the Url Alias (entityUrl.path)?
I am using https://github.com/drupal-graphql/graphql
I can do a bulk query for all the articles, do I then filter those results?
Thanks
query GetArticles {
nodeQuery(filter: {conditions: [{field: "type", value: "article"}]}) {
Articles: entities {
entityId
entityLabel
entityUrl {
path
routed
}
}
}
}
Figured it out
query ($path: String!) {
route:route(path: $path) {
... on EntityCanonicalUrl {
entity {
... on Node {
nid
entityLabel
body{
value
}
}
}
}
}
}

Github GraphQL Repository Query, using two objects

How to use more than one objects when querying Github GraphQL?
The following will break if the 2nd object is uncommented:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
}
# object(expression: "master:README.md") {... on Blob {byteSize}}
}
}
How to make it working? Thx
Use alias:
query {
repository(owner:"rails", name:"rails") {
object(expression:"master") {
... on Commit {
history {
totalCount
}
}
},
second_object: object(expression: "master:README.md") {... on Blob {byteSize}}
}
}

Resources