-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix plotting series when the first data point is empty.
- Add `graphql` to the dependencies, to help interacting with the schema. - Get the schema in parallel with the first data query and remember it. - Use it to find the numeric fields under the data paths, instead of inspecting the first data point.
- Loading branch information
Tony Hignett
committed
May 25, 2021
1 parent
994dfac
commit 481bbd7
Showing
9 changed files
with
170 additions
and
3,225 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { GraphQLFloat, GraphQLInt, GraphQLObjectType, GraphQLString } from 'graphql'; | ||
import { Schema } from './schema'; | ||
|
||
test('getTypeOfDescendant', () => { | ||
const childType = new GraphQLObjectType({ | ||
name: 'child-type', | ||
fields: { | ||
grandchild1: { type: GraphQLInt }, | ||
grandchild2: { type: GraphQLFloat }, | ||
}, | ||
}); | ||
const parentType = new GraphQLObjectType({ | ||
name: 'parent-type', | ||
fields: { | ||
child1: { type: GraphQLString }, | ||
child2: { | ||
type: childType, | ||
}, | ||
}, | ||
}); | ||
|
||
expect(Schema.getTypeOfDescendant(parentType, 'child1')).toBe(GraphQLString); | ||
expect(Schema.getTypeOfDescendant(parentType, 'child2')).toBe(childType); | ||
expect(Schema.getTypeOfDescendant(parentType, 'child2.grandchild1')).toBe(GraphQLInt); | ||
expect(Schema.getTypeOfDescendant(parentType, 'child2.grandchild2')).toBe(GraphQLFloat); | ||
expect(Schema.getTypeOfDescendant(childType, 'grandchild1')).toBe(GraphQLInt); | ||
}); | ||
|
||
describe('isNumericType', () => { | ||
test('object', () => { | ||
const type = new GraphQLObjectType({ name: 'Address', fields: { street: { type: GraphQLString } } }); | ||
expect(Schema.isNumericType(type)).not.toBeTruthy(); | ||
}); | ||
|
||
test('string', () => { | ||
expect(Schema.isNumericType(GraphQLString)).not.toBeTruthy(); | ||
}); | ||
|
||
test('int', () => { | ||
expect(Schema.isNumericType(GraphQLInt)).toBeTruthy(); | ||
}); | ||
|
||
test('float', () => { | ||
expect(Schema.isNumericType(GraphQLFloat)).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { | ||
buildClientSchema, | ||
getIntrospectionQuery, | ||
getNamedType, | ||
GraphQLNamedType, | ||
GraphQLObjectType, | ||
isObjectType, | ||
isScalarType, | ||
printSchema, | ||
} from 'graphql'; | ||
import { RequestFactory } from './types'; | ||
|
||
export class Schema { | ||
private query: Promise<GraphQLObjectType> | undefined; | ||
|
||
constructor(private requestFactory: RequestFactory) { | ||
this.requestFactory = requestFactory; | ||
} | ||
|
||
getQuery(): Promise<GraphQLObjectType> { | ||
if (!this.query) { | ||
this.query = this.requestFactory.request(getIntrospectionQuery()).then((results: any) => { | ||
let schema = buildClientSchema(results.data.data); | ||
let queryType = schema.getQueryType(); | ||
if (!queryType) { | ||
throw `No query type in schema: ${printSchema(schema)}`; | ||
} | ||
return queryType; | ||
}); | ||
} | ||
// @ts-ignore (it's defined now) | ||
return this.query; | ||
} | ||
|
||
static getTypeOfDescendant(nodeType: GraphQLObjectType, path: string): GraphQLNamedType { | ||
let descendantType = nodeType; | ||
let pathComponents = path.split('.'); | ||
for (let i = 0; i < pathComponents.length; i++) { | ||
let type = getNamedType(descendantType.getFields()[pathComponents[i]].type); | ||
if (i === pathComponents.length - 1) { | ||
return type; | ||
} else { | ||
if (!isObjectType(type)) { | ||
throw `Found type ${type.name} for component ${pathComponents[i]} of ${path}, expected object type`; | ||
} | ||
descendantType = type as GraphQLObjectType; | ||
} | ||
} | ||
return descendantType; | ||
} | ||
|
||
static isNumericType(fieldType: GraphQLNamedType): boolean { | ||
return isScalarType(fieldType) && (fieldType.name === 'Int' || fieldType.name === 'Float'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters