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

allow reference input type #1185

Closed
ghost opened this issue Aug 24, 2018 · 4 comments
Closed

allow reference input type #1185

ghost opened this issue Aug 24, 2018 · 4 comments

Comments

@ghost
Copy link

ghost commented Aug 24, 2018

i m try to return type but all Sacalrtype is working fine but try to return foreign type like

public GraphQLInputType getInputAttributeType(Attribute<?, ?> attribute) {
    	if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_MANY || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_MANY) {
            EntityType<?> foreignType = (EntityType<?>) ((PluralAttribute<?, ?, ?>) attribute).getElementType();
            return new GraphQLList(new GraphQLTypeReference(foreignType.getName()));
        } else if (attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.MANY_TO_ONE || attribute.getPersistentAttributeType() == Attribute.PersistentAttributeType.ONE_TO_ONE) {
        	EntityType<?> foreignType = (EntityType<?>) ((SingularAttribute<?, ?>) attribute).getType();
            return new GraphQLTypeReference(foreignType.getName());
        } 
    	
    	final String declaringType = attribute.getDeclaringType().getJavaType().getName(); // fully qualified name of the entity class
        final String declaringMember = attribute.getJavaMember().getName(); // field name in the entity class

        throw new UnsupportedOperationException(
                "Attribute could not be mapped to GraphQL: field '" + declaringMember + "' of entity class '" + declaringType + "'");
    }

so it give error like java.lang.ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType

@ghost
Copy link
Author

ghost commented Aug 27, 2018

i want to add reference field like

 public GraphQLInputObjectType getGraphqlInputType(EntityType<?> entityType) {
    	GraphQLInputObjectType.Builder inputType = GraphQLInputObjectType.newInputObject().name(entityType.getName().toLowerCase());
    	inputType.field(GraphQLInputObjectField.newInputObjectField().name("testList").type(GraphQLTypeReference.typeRef("Address"))).build();
    	return inputType.build();
    }

but it give error like

java.lang.ClassCastException: graphql.schema.GraphQLObjectType cannot be cast to graphql.schema.GraphQLInputType

so how to done referencetype in GraphqlInputType ??

@tinnou
Copy link
Contributor

tinnou commented Aug 28, 2018

Would you be able to post a stack trace and put together a small unit test that compiles? Otherwise it will be hard to help.

That said, my feeling here is you might already have registered on your schema the Address type as an output object type and you're trying to register a field on an input type of type object type Address. However, remember per the spec that all fields on a input type must be input types, scalars or enums. i.e you can't use a field of object type on an input type.

GraphQL specification - Input Objects:

A GraphQL Input Object defines a set of input fields; the input fields are either scalars, enums, or other input objects.

@bbakerman
Copy link
Member

Even if he has misconfigured his schema but putting an GraphqlObjectType in play we still need to double check that graphql-java does indeed allow circular Input objects, which in java are only possible via a TypeRef style indirection layer (unlike JS)

See graphql/graphql-spec#189 and graphql/graphql-js#1359

I think we should double check that you can in fact do the following in graphql-java

 input Something {
       name : String
       otherThing : Something
 }

@bbakerman
Copy link
Member

Ok I have double check that GraphqlTypeReferences can be used with input object types and yes they can. I proved it with the above self referential type.

I suspect the original report has a mix of output object types in his input objects types which is not allowed in graphql

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

2 participants