Skip to content

Conversation

@yarinvak
Copy link
Member

@yarinvak yarinvak commented Oct 12, 2017

Adds support for GraphQLID (exists in graphql-java).
Adds an annotation @GraphQLID you can put before a method/field with String/Integer/int type.
For example:
private @GraphQLID String id;
It will be represented as an ID scalar.

Fixes issue #71

Added type annotation @GraphQLID
@guy120494
Copy link
Contributor

What happens if I put @GraphQLID on 2 fields? (2 on one class, or one in a class and one in a class that extends (type extension) the original class)

}

@Test
public void id() throws NoSuchMethodException, NoSuchFieldException {
Copy link
Contributor

Choose a reason for hiding this comment

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

Meaningfull name please. What does this test do?

Field idIntegerField = DefaultTypeFunctionTest.class.getField("idIntegerField");
Field idIntField = DefaultTypeFunctionTest.class.getField("idIntField");

assertEquals(instace.buildType(idStringMethod.getReturnType(), idStringMethod.getAnnotatedReturnType()), GraphQLID);
Copy link
Contributor

Choose a reason for hiding this comment

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

Alot of asserts. Maybe better to split to few tests

@yarinvak
Copy link
Member Author

yarinvak commented Oct 12, 2017

If you put @GraphQLIDon 2 fields, they'll both be represented as IDs. I dont think there is some restriction for it on the graphql level (correct me if im wrong)

@guy120494
Copy link
Contributor

From the official docs:

ID: The ID scalar type represents a unique identifier...

So, I think there should be only 1 ID per object

@guy120494
Copy link
Contributor

On second thought, the uniqueness is of the value of the id, not the id itself, so it is fine by me that an object can have 2 ID's

@Override
public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
return annotatedType != null
&& (aClass == Integer.class || aClass == int.class || aClass == String.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you restrict the ID to be a String or an int? What If I want my ID to be a costum type? (UUID for example)

Copy link
Member Author

@yarinvak yarinvak Oct 13, 2017

Choose a reason for hiding this comment

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

Because the spec defines GraphQLID to be integer/string, it can only accept those 2 types:

When expected as an input type, any string (such as "4") or integer (such as 4) input value should be coerced to ID as appropriate for the ID formats a given GraphQL server expects. Any other input value, including float input values (such as 4.0), must raise a query error indicating an incorrect type.

public boolean canBuildType(Class<?> aClass, AnnotatedType annotatedType) {
return annotatedType != null
&& (aClass == Integer.class || aClass == int.class || aClass == String.class)
&& (annotatedType.getAnnotation(GraphQLID.class) != null);
Copy link

Choose a reason for hiding this comment

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

You shouldn't use getAnnotation(GraphQLID.class) to verify that the annotation is present. It may throw a NullPointerExeception. You should use instead isAnnotationPresent(GraphQLID.class).

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right, thanx :)

@guy120494 guy120494 merged commit 9a600e1 into master Oct 15, 2017
@guy120494 guy120494 deleted the GraphQLID-support branch October 15, 2017 06:43
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.

4 participants