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

Type validation when using variables #2

Closed
IcanDivideBy0 opened this issue Sep 9, 2016 · 4 comments
Closed

Type validation when using variables #2

IcanDivideBy0 opened this issue Sep 9, 2016 · 4 comments

Comments

@IcanDivideBy0
Copy link

IcanDivideBy0 commented Sep 9, 2016

Hello,
When using variables in query, the inputType never get validated.
Assuming we're using your Star Wars schema, the following will work, whatever the side input in variables :

var HeroInputType = UnionInputType({
    name: 'hero',
    inputTypes: [JediInputType, SithInputType], //an object can be used instead to query by names other than defined in these types
    typeKey: 'side'
});

[...]

graphql(Schema, `
  mutation($hero: HeroInputType!) {
    hero(input: $hero) {
      ...
    }
  }
`, {}, {}, {
  hero: {
    side: 'WHATEVER'
  }
});
@Cardinal90
Copy link
Owner

Last line of the Documentation: "Note that this only validates literal values, variables are assumed to provide values of the correct type." While it is possible to check a literal value, I don't see a way to access variable values during validation. Seems to be a limitation of this method.

If you really need it, the only option I see is to somehow manually insert a variable into the query string on the server before calling graphql function. Did not try it, there could be some caveats.

@taion
Copy link

taion commented Apr 6, 2017

Perhaps I'm misunderstanding, but couldn't you do this in parseValue?

@maddijoyce
Copy link

maddijoyce commented May 17, 2017

Hi Cardinal90, I managed to get this going in parseValue as taion mentioned. I'm not sure if the way I did it was particularly correct, but I did it like this:

parseValue: function(value) {
	var inputType, ast;
	if (typeKey && value[typeKey]) {
		inputType = referenceTypes[value[typeKey]];
	}
	ast = astFromValue(value, inputType);
	return valueFromAST(ast, inputType);
},

Obviously this requires you to only use the typeKey method, but I assume it could be made to work with the other techniques as well?

@Cardinal90
Copy link
Owner

Hello. Sorry for not replying for some time.
Seems that I didn't do my research properly. You were right. I updated the package, it should fully support variables.
Please note, that you need to use the type name (name option) as the variable type, and not a variable name. For example, in IcanDivideBy0's message it should have been mutation($hero: hero!).

On another note, graphql-js 0.10.1 broke my tests. I reported this here. It shouldn't cause any real problems, at least if your inputs don't have mistakes in them.

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

No branches or pull requests

4 participants