Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to define double nested array in schema #423

Closed
takotuesday opened this issue Mar 8, 2018 · 3 comments

Comments

Projects
None yet
3 participants
@takotuesday
Copy link

commented Mar 8, 2018

I am trying to do the following:

// Schema definitions
const typeDefs = `
type Chart {
  name: String
  series: [Series]
}

type Series {
  name: String!
  data: [[Int, Float]]
}
`

where Series.data is an array of data points. Each data point is an array of x,y coordinates. For this use case a point cannot be an object with x,y keys, it must be an array. This fails to build because GraphQL does not like the definition of data: [[Int, Float]]. I have scoured the internet, GitHub, and stackoverflow. What is the solution/workaround for this? Thanks

@IvanGoncharov

This comment has been minimized.

Copy link
Member

commented Mar 8, 2018

GraphQL doesn't support tuples, but you can write this:

type Chart {
  name: String
  series: [Series]
}

type Series {
  name: String!
  data: [DataPoint]
}

type DataPoint {
  x: Int
  y: Float
}
@takotuesday

This comment has been minimized.

Copy link
Author

commented Mar 8, 2018

@IvanGoncharov thank you for the prompt response. Unfortunately as I mentioned in my original post, For this use case a point cannot be an object with x,y keys, it must be an array. The charting framework I'm using on the front end requires tuples for large data sets. The only work around with your suggestion is to normalize the data on the front end, converting a point from an object to an array. This is not performant and slows down the page load. Are you aware of a custom scalar tuple type? It seems like my use case shouldn't be that rare.

@IvanGoncharov

This comment has been minimized.

Copy link
Member

commented Mar 9, 2018

For this use case a point cannot be an object with x,y keys, it must be an array

Missed that. Another less than ideal solutions is:

type Series {
  name: String!
  data: [[Float]]
}

Are you aware of a custom scalar tuple type? It seems like my use case shouldn't be that rare.

My opinion is that GraphQL is more about modeling data than to adapting data to particular library/framework. If you think that GraphQL must have tuple support you need to answer this questionary:
image

Here is Lee talk where he explains every question: https://www.youtube.com/watch?v=mePT9MNTM98&feature=youtu.be&t=20m32s

  1. Can we enable it without a change to GraphQL?
  2. If so how awkward is it?

Personally, I think there is nothing awkward about having:

type DataPoint {
  x: Int
  y: Float
}

If you think differently please open a separate issue about adding tuple support to GraphQL and include answers to above questions into the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.