我尝试用一个对象数组映射一个键字符串。
我可以创建一个简单的对象,但是我想在这些数组中轻松添加一个对象。 Map Object非常适合执行此操作。
问题:我不知道如何为GraphQL定义类型图:'(
@ObjectType()
export class Inventaire
@Field()
_id: string;
@Field()
stocks: Map<string, Article[]>;
}
答案 0 :(得分:1)
GraphQL是一种强类型语言,不提供任何现成的 map 类型。键值对的JSON Blob没有强大的架构,因此您不能有以下内容:
{
key1: val1,
key2: val2,
key3: val3,
...
}
但是,您可以定义GraphQL模式以具有键值元组类型,然后定义属性以返回这些元组的数组。
type articleMapTuple {
key: String
value: Article
}
type Inventaire {
stocks: [articleMapTuple]
}
然后您的返回类型将如下所示:
data [
{
key: foo1,
value: { some Article Object}
},
{
key: foo2,
value: { some Article Object}
},
{
key: foo3,
value: { some Article Object}
},
]
答案 1 :(得分:0)
您可以使用此软件包https://www.npmjs.com/package/graphql-type-json。
示例:
import { makeExecutableSchema } from 'graphql-tools';
import GraphQLJSON, { GraphQLJSONObject } from 'graphql-type-json';
const typeDefs = `
scalar JSON
scalar JSONObject
type MyType {
myValue: JSON
myObject: JSONObject
}
# ...
`;
const resolvers = {
JSON: GraphQLJSON,
JSONObject: GraphQLJSONObject,
};
export default makeExecutableSchema({ typeDefs, resolvers });
答案 2 :(得分:0)
GraphQL本机不支持Map类型。您可以为Map创建自己的标量,也可以使用repo https://github.com/graphql-java/graphql-java-extended-scalars
中定义的现有ObjectScalar
import graphql.Assert;
import graphql.language.ArrayValue;
import graphql.language.BooleanValue;
import graphql.language.EnumValue;
import graphql.language.FloatValue;
import graphql.language.IntValue;
import graphql.language.NullValue;
import graphql.language.ObjectValue;
import graphql.language.StringValue;
import graphql.language.Value;
import graphql.language.VariableReference;
import graphql.language.ObjectField;
import graphql.scalars.util.Kit;
import graphql.schema.Coercing;
import graphql.schema.CoercingParseLiteralException;
import graphql.schema.CoercingParseValueException;
import graphql.schema.CoercingSerializeException;
import graphql.schema.GraphQLScalarType;
import org.springframework.stereotype.Component;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class ObjectScalar extends GraphQLScalarType {
public ObjectScalar() {
this("Object", "An object scalar");
}
ObjectScalar(String name, String description) {
super(name, description, new Coercing<Object, Object>() {
public Object serialize(Object input) throws CoercingSerializeException {
return input;
}
public Object parseValue(Object input) throws CoercingParseValueException {
return input;
}
public Object parseLiteral(Object input) throws CoercingParseLiteralException {
return this.parseLiteral(input, Collections.emptyMap());
}
public Object parseLiteral(Object input, Map<String, Object> variables)
throws CoercingParseLiteralException {
if (!(input instanceof Value)) {
throw new CoercingParseLiteralException("Expected AST type 'StringValue' but" +
" was '" + Kit.typeName(input) + "'.");
} else if (input instanceof NullValue) {
return null;
} else if (input instanceof FloatValue) {
return ((FloatValue)input).getValue();
} else if (input instanceof StringValue) {
return ((StringValue)input).getValue();
} else if (input instanceof IntValue) {
return ((IntValue)input).getValue();
} else if (input instanceof BooleanValue) {
return ((BooleanValue)input).isValue();
} else if (input instanceof EnumValue) {
return ((EnumValue)input).getName();
} else if (input instanceof VariableReference) {
String varName = ((VariableReference)input).getName();
return variables.get(varName);
} else {
List values;
if (input instanceof ArrayValue) {
values = ((ArrayValue)input).getValues();
return values.stream().map((v) -> {
return this.parseLiteral(v, variables);
}).collect(Collectors.toList());
} else if (input instanceof ObjectValue) {
values = ((ObjectValue)input).getObjectFields();
Map<String, Object> parsedValues = new LinkedHashMap();
values.forEach((fld) -> {
Object parsedValue = this.parseLiteral(((ObjectField)fld).getValue(),
variables);
parsedValues.put(((ObjectField)fld).getName(), parsedValue);
});
return parsedValues;
} else {
return Assert.assertShouldNeverHappen("We have covered all Value types",
new Object[0]);
}
}
}
});
}
}
scalar Object
type Result {
value : Object
}