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

Update the jsonField directive to have an option for disabling the index #1613

Merged
merged 2 commits into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions packages/utils/src/graphql/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import {
isNonNullType,
isObjectType,
isUnionType,
ValueNode,
BooleanValueNode,
} from 'graphql';
import {getTypeByScalarName} from '../types';
import {DirectiveName} from './constant';
Expand Down Expand Up @@ -120,16 +122,20 @@ export function getAllEntitiesRelations(_schema: GraphQLSchema | string): GraphQ
}
// If is jsonField
else if (jsonObjects.map((json) => json.name).includes(typeString)) {
const jsonObject = setJsonObjectType(
jsonObjects.find((object) => object.name === typeString),
jsonObjects
);
newModel.fields.push(packJSONField(typeString, field, jsonObject));
newModel.indexes.push({
unique: false,
fields: [field.name],
using: IndexType.GIN,
});
const jsonObject = jsonObjects.find((object) => object.name === typeString);
const jsonObjectType = setJsonObjectType(jsonObject, jsonObjects);
newModel.fields.push(packJSONField(typeString, field, jsonObjectType));

const directive = jsonObject.astNode.directives.find(({name: {value}}) => value === DirectiveName.JsonField);
const argValue = directive?.arguments?.find((arg) => arg.name.value === 'indexed')?.value;
// For backwards compatibility if the argument is not defined then the index will be added
if (!argValue || (isBooleanValueNode(argValue) && argValue.value !== false)) {
newModel.indexes.push({
unique: false,
fields: [field.name],
using: IndexType.GIN,
});
}
} else {
throw new Error(`${typeString} is not an valid type`);
}
Expand Down Expand Up @@ -254,3 +260,7 @@ function validateRelations(modelRelations: GraphQLModelsRelationsEnums): void {
);
}
}

function isBooleanValueNode(valueNode?: ValueNode): valueNode is BooleanValueNode {
return valueNode?.kind === 'BooleanValue';
}
28 changes: 28 additions & 0 deletions packages/utils/src/graphql/graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,4 +263,32 @@ describe('utils that handle schema.graphql', () => {
expect(accountModel.fields[0].jsonInterface.fields[2].type).toBe('Json');
expect(accountModel.fields[0].jsonInterface.fields[2].jsonInterface.name).toBe('MyJson2');
});

it('can read jsonfield with indexed option', () => {
const graphqlSchema = gql`
type MyJson @jsonField(indexed: false) {
data: String!
data2: [String]
}
type MyJson2 @jsonField(indexed: true) {
data: String!
data2: [String]
}
type MyJson3 @jsonField {
data: String!
data2: [String]
}
type Account @entity {
field1: MyJson!
field2: MyJson2!
field3: MyJson3!
}
`;
const schema = buildSchemaFromDocumentNode(graphqlSchema);
const entities = getAllEntitiesRelations(schema);

expect(entities.models?.[0].indexes.length).toEqual(2);
expect(entities.models?.[0].indexes[0].fields).toEqual(['field2']);
expect(entities.models?.[0].indexes[1].fields).toEqual(['field3']);
});
});
2 changes: 1 addition & 1 deletion packages/utils/src/graphql/schema/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import gql from 'graphql-tag';
export const directives = gql`
directive @derivedFrom(field: String!) on FIELD_DEFINITION
directive @entity on OBJECT
directive @jsonField on OBJECT
directive @jsonField(indexed: Boolean) on OBJECT
directive @index(unique: Boolean) on FIELD_DEFINITION
`;