-
Notifications
You must be signed in to change notification settings - Fork 4
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
Add filter types to schema #21
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.graphqlcrud; | ||
|
||
import graphql.Scalars; | ||
import graphql.schema.*; | ||
|
||
public class Filters { | ||
|
||
public static GraphQLInputObjectType.Builder pageInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("PageRequest") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("limit").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("offset").type(Scalars.GraphQLInt)); | ||
} | ||
|
||
public static GraphQLInputObjectType.Builder filterInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("QueryFilter") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("id").type(GraphQLTypeReference.typeRef("IDInput"))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("title").type(GraphQLTypeReference.typeRef("StringInput"))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("clickCount").type(GraphQLTypeReference.typeRef("IntInput"))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now using these types add FilterInput on Customer like
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding it additionally or modifying the existing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As a separate type, see https://graphqlcrud.org/docs/next/find and example of |
||
.field(GraphQLInputObjectField.newInputObjectField().name("floatValue").type(GraphQLTypeReference.typeRef("FloatInput"))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("description").type(GraphQLTypeReference.typeRef("StringInput"))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("and").type(GraphQLList.list(GraphQLTypeReference.typeRef("QueryFilter")))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("or").type(GraphQLList.list(GraphQLTypeReference.typeRef("QueryFilter")))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("not").type(GraphQLTypeReference.typeRef("QueryFilter"))); | ||
} | ||
|
||
public static GraphQLInputObjectType.Builder stringInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("StringInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ne").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("eq").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("le").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("lt").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ge").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("gt").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("in").type(GraphQLList.list(GraphQLNonNull.nonNull(Scalars.GraphQLString)))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("contains").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("startsWith").type(Scalars.GraphQLString)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("endsWith").type(Scalars.GraphQLString)); | ||
|
||
} | ||
|
||
public static GraphQLInputObjectType.Builder idInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("IDInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ne").type(Scalars.GraphQLID)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("eq").type(Scalars.GraphQLID)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("in").type(GraphQLList.list(GraphQLNonNull.nonNull(Scalars.GraphQLID)))); | ||
|
||
} | ||
|
||
public static GraphQLInputObjectType.Builder booleanInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("BooleanInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ne").type(Scalars.GraphQLBoolean)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("eq").type(Scalars.GraphQLBoolean)); | ||
} | ||
|
||
public static GraphQLInputObjectType.Builder floatInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("FloatInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ne").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("eq").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("le").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("lt").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ge").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("gt").type(Scalars.GraphQLFloat)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("in").type(GraphQLList.list(GraphQLNonNull.nonNull(Scalars.GraphQLFloat)))); | ||
} | ||
|
||
public static GraphQLInputObjectType.Builder intInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("IntInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ne").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("eq").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("le").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("lt").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("ge").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("gt").type(Scalars.GraphQLInt)) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("in").type(GraphQLList.list(GraphQLNonNull.nonNull(Scalars.GraphQLInt)))); | ||
} | ||
|
||
public static GraphQLInputObjectType.Builder orderByInputBuilder() { | ||
return GraphQLInputObjectType.newInputObject().name("OrderByInput") | ||
.field(GraphQLInputObjectField.newInputObjectField().name("field").type(GraphQLNonNull.nonNull(Scalars.GraphQLString))) | ||
.field(GraphQLInputObjectField.newInputObjectField().name("order").type(GraphQLTypeReference.typeRef("SortDirectionEnum")).defaultValue("ASC")); | ||
} | ||
|
||
public static GraphQLEnumType.Builder sortDirectionEnumBuilder() { | ||
return GraphQLEnumType.newEnum().name("SortDirectionEnum") | ||
.value("ASC") | ||
.value("DESC"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,23 +18,15 @@ | |
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import graphql.schema.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import graphql.Scalars; | ||
import graphql.language.OperationTypeDefinition; | ||
import graphql.language.SchemaDefinition; | ||
import graphql.language.TypeName; | ||
import graphql.schema.FieldCoordinates; | ||
import graphql.schema.GraphQLArgument; | ||
import graphql.schema.GraphQLCodeRegistry; | ||
import graphql.schema.GraphQLFieldDefinition; | ||
import graphql.schema.GraphQLList; | ||
import graphql.schema.GraphQLNonNull; | ||
import graphql.schema.GraphQLObjectType; | ||
import graphql.schema.GraphQLObjectType.Builder; | ||
import graphql.schema.GraphQLSchema; | ||
import graphql.schema.GraphQLTypeReference; | ||
import io.graphqlcrud.model.Entity; | ||
import io.graphqlcrud.model.Schema; | ||
import io.graphqlcrud.types.JdbcTypeMap; | ||
|
@@ -61,9 +53,19 @@ public GraphQLSchema buildSchema(Schema schema) { | |
GraphQLObjectType.Builder queryTypeBuilder = GraphQLObjectType.newObject(); | ||
queryTypeBuilder.name("QueryType"); | ||
|
||
//add filters for queries | ||
builder.additionalType(Filters.pageInputBuilder().build()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Awesome. I think it will be amazing to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are printing the schema using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running this now! thank you |
||
builder.additionalType(Filters.stringInputBuilder().build()); | ||
builder.additionalType(Filters.intInputBuilder().build()); | ||
builder.additionalType(Filters.idInputBuilder().build()); | ||
builder.additionalType(Filters.booleanInputBuilder().build()); | ||
builder.additionalType(Filters.floatInputBuilder().build()); | ||
builder.additionalType(Filters.sortDirectionEnumBuilder().build()); | ||
builder.additionalType(Filters.orderByInputBuilder().build()); | ||
builder.additionalType(Filters.filterInputBuilder().build()); | ||
|
||
schema.getEntities().stream().forEach(entity -> { | ||
GraphQLObjectType.Builder typeBuilder = GraphQLObjectType.newObject(); | ||
//TODO : Model Annotations | ||
typeBuilder.description(entity.getDescription()); | ||
typeBuilder.name(entity.getName()); | ||
typeBuilder.withDirective(SQLDirective.newDirective().tablename(entity.getFullName()).build()); | ||
|
@@ -101,6 +103,9 @@ private void addQueryOperationsForEntity(Entity entity, Builder queryTypeBuilder | |
String name = StringUtil.plural(entity.getName()).toLowerCase(); | ||
GraphQLFieldDefinition.Builder builder = GraphQLFieldDefinition.newFieldDefinition(); | ||
builder.name(name); | ||
builder.argument(GraphQLArgument.newArgument().name("page").type(GraphQLTypeReference.typeRef("PageRequest")).build()); | ||
builder.argument(GraphQLArgument.newArgument().name("filter").type(GraphQLTypeReference.typeRef("QueryFilter")).build()); | ||
builder.argument(GraphQLArgument.newArgument().name("orderBy").type(GraphQLTypeReference.typeRef("OrderByInput")).build()); | ||
builder.type(GraphQLList.list(new GraphQLTypeReference(entity.getName()))); | ||
queryTypeBuilder.field(builder.build()); | ||
codeBuilder.dataFetcher(FieldCoordinates.coordinates("QueryType", name), DEFAULT_DATA_FETCHER_FACTORY); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,10 @@ public void testSchemaPrint() throws Exception { | |
Assertions.assertNotNull(accountsDef.getDirective("sql")); | ||
Assertions.assertEquals("[SSN]", accountsDef.getDirective("sql").getArgument("keys").getValue().toString()); | ||
Assertions.assertEquals("[SSN]", accountsDef.getDirective("sql").getArgument("reference_keys").getValue().toString()); | ||
|
||
GraphQLFieldDefinition fieldDefinition = objectType.getFieldDefinition("customers"); | ||
Assertions.assertEquals("page",fieldDefinition.getArguments().get(0).getName()); | ||
Assertions.assertEquals("filter", fieldDefinition.getArguments().get(1).getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you expand the test to show to full filter and its arguments |
||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this some form of sandbox. It contains business model where it should be generic?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on the schema we read from the Database we are applying the where filters and page limits apply automatically.