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

JSON Object/Map is not a valid input type #509

Open
UkonnRa opened this issue Jan 28, 2020 · 11 comments
Open

JSON Object/Map is not a valid input type #509

UkonnRa opened this issue Jan 28, 2020 · 11 comments
Labels
bug Something isn't working needs-triage

Comments

@UkonnRa
Copy link

UkonnRa commented Jan 28, 2020

Describe the bug
In GraphQL Java, JSON Scalar is supported, so I can write the following query:

query {
        user(input : {
                id : "216ae7d9-cbe7-4d6e-9968-33e3424da0fe",
                badges : { Github : "http://example.com" }
        }) {
            id
            email
        }
}

But the same code in juniper will throw an error:

ParseError(Spanning { item: UnexpectedToken(CurlyOpen), start: SourcePosition { index: 94, line: 1, col: 83 }, end: SourcePosition { index: 95, line: 1, col: 84 } })

So JSON Object in input is not suppoerted yet?

@UkonnRa UkonnRa added bug Something isn't working needs-triage labels Jan 28, 2020
@davidpdrsn
Copy link
Contributor

Why not just use a regular structured input type?

@UkonnRa
Copy link
Author

UkonnRa commented Jan 28, 2020

Because I want to send something like: HashMap<String, String>. Any advice?

@davidpdrsn
Copy link
Contributor

You could do something like this:

mutation {
  foo(pairs: [Pair!]!): Boolean
}

type Pair {
  key: String!
  value: String!
}

@UkonnRa
Copy link
Author

UkonnRa commented Jan 28, 2020

Cool! That's may help in my project. But I still hope that JSON Scalar in Input can be in consideration.

@UkonnRa
Copy link
Author

UkonnRa commented Jan 28, 2020

Wait, still get the ParseError even I change to the array? I cannot figure out where is going wrong... @davidpdrsn

mutation {
    createUser(input: {nickname: "nickname", avatar: "http://example.com", badges: [{key: "Github", value: "https://github.com"}]}) {
        id
        nickname
        badges
    }
}

Error:

called `Result::unwrap()` on an `Err` value: ParseError(Spanning { item: UnexpectedToken(CurlyOpen), start: SourcePosition { index: 95, line: 1, col: 84 }, end: SourcePosition { index: 96, line: 1, col: 85 } })
thread 'user::endpoint::graphql::tests::test_graphql' panicked at 'called `Result::unwrap()` on an `Err` value: ParseError(Spanning { item: UnexpectedToken(CurlyOpen), start: SourcePosition { index: 95, line: 1, col: 84 }, end: SourcePosition { index: 96, line: 1, col: 85 } })', src\libcore\result.rs:1165:5

@davidpdrsn
Copy link
Contributor

This works for me

# Schema. You'll have to translate this into rust yourself but it should be pretty straight forward
type Mutation {
  foo(pairs: [Pair!]!): Boolean!
}

input Pair {
  key: String!
  value: String!
}

# Query
mutation {
  foo(pairs: [{key: "foo", value: "bar"}, {key: "foo", value: "bar"}])
}

Regarding allowing raw JSON as input type. I guess it would make sense if the spec says so, otherwise I would be reluctant to adding it. What do you think @theduke @LegNeato?

@UkonnRa
Copy link
Author

UkonnRa commented Jan 28, 2020

Now it works! It seems that there are some problems when writing a Scalar manually, so I just change the data structure. Sorry for bothering :-)

@LegNeato
Copy link
Member

LegNeato commented Jan 29, 2020

See graphql/graphql-spec#584 for the RFC to support this I believe. I haven't seen where that RFC ended up though.

@LegNeato
Copy link
Member

Also graphql/graphql-spec#627.

@UkonnRa
Copy link
Author

UkonnRa commented Feb 21, 2020

Well, I think those are two different things. The RFC want to implement ADT in GraphQL DSL, which is way much harder. What I'm wanting is just a general JSON scalar type

@UkonnRa
Copy link
Author

UkonnRa commented Feb 21, 2020

In fact, #325 is exactlly what I want, hoping this PR can move on...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage
Projects
None yet
Development

No branches or pull requests

3 participants