Skip to content

Null As for graphql #68

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

Merged
merged 6 commits into from
May 26, 2018
Merged

Conversation

theishshah
Copy link
Member

Addressing #62

I added the flag for --null-as and made some assumptions about how the implementation should work, however it should be trivial to switch that logic if you have a better/different idea in mind

@thomasdfischer
Copy link
Member

The flag code looks good. To debug for this PR, workflow will be something like this:

$ schema infer --graphql
{
    "name": "Tom",
    "email": null
}^D
error: failed to serialize schema
cannot infer type of null value (see key 'email')

^ that demonstrates how schema works on master.

We want to be able to explicitly tell schema to use the type string/bool/number in place of null values in the generated graphql schema. When --null-as is implemented, users should be able to do this:

$ schema infer --graphql --null-as=string
{
    "name": "Tom",
    "email": null
}^D
type Object {
    name: String!
    email: String!
}

@theishshah
Copy link
Member Author

I think there is an issue when building the schema map that is disjoint from the actual build schema stuff I changed, which is causing a basic test I wrote to fail, unsure of how best to go forward

@@ -10,7 +10,7 @@ import (
func buildSchema(fromValue interface{}, dst *interface{}, params *FromExampleParams) error {
switch v := fromValue.(type) {
case nil:
*dst = NewNull()
*dst = NewNull(params)
Copy link
Member

@thomasdfischer thomasdfischer May 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema infer --graphql works like this right now

  1. Read incoming JSON/YAML/TOML, etc.
  2. Create an jsonsch.Schema object in memory
  3. Call the method js.ToGraphQLSchema() to convert it to a graphqlsch.Schema object
  4. Serialize the graphqlsch.Schema object

All the logic of this PR looks correct, but it's in part 2 (from the numbering above^) of the codebase. Instead, part 3 should be modified. The lines we need to modify are here:

call to handlePrimitive: https://github.com/Confbase/schema/blob/master/jsonsch/jsonsch.go#L32

actual line that rejects null values in handlePrimitive: https://github.com/Confbase/schema/blob/master/jsonsch/jsonsch.go#L96

call to handleArraySchema: https://github.com/Confbase/schema/blob/master/jsonsch/jsonsch.go#L44

actual line that rejects null values: https://github.com/Confbase/schema/blob/master/jsonsch/jsonsch.go#L140

The codebase has zero documentation right now, so no question is a dumb question. Feel free to yell at me if anything is confusing

@@ -1750,6 +1750,16 @@ infer_json_null_graphql() {
expect=`printf "error: failed to serialize schema\ncannot infer type of null value (see key 'is2004')"`
}

infer_json_as_null_string_graphql() {
output=`printf '{"myString":null}' | schema infer --graphql --as-null=string --omit-required=false 2>&1`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test looks good except we don't need --omit-required=false since it has no bearing on graphql schemas. When that flag is true, the "required" field is omitted from JSON schemas.

@theishshah
Copy link
Member Author

Alright, I think this should be good to merge 😄

@thomasdfischer thomasdfischer merged commit 848b773 into Confbase:master May 26, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants