1

我正在尝试使用 SPQR 从 Cayenne 生成的类生成 GraphQL 模式。Cayenne 类看起来像这样

public class MyCayenneClass {
  public static final Property<Integer> A_PROPERTY = Property.create("aProperty", Integer.class);
  public static final Property<Integer> ANOTHER_PROPERTY = Property.create("anotherProperty", String.class);

  public void setAProperty(Integer aProperty) {
      writeProperty("aProperty", aProperty);
  }
  public Integer getAProperty() {
      return (Integer)readProperty("aProperty");
  }

  public void setAnotherProperty(String anotherProperty) {
      writeProperty("anotherProperty", anotherProperty);
  }
  public String getAnotherProperty() {
      return (String)readProperty("anotherProperty");
  }
}

由于该类不是简单的 POJO,因此 SPQR 会引发异常并且不会生成模式。

Error: QUERY_ROOT fields must be an object with field names as keys or a function which returns such an object.

这里最好的方法是什么(不修改 cayenne 类(即注释方法)?

GraphQLEndPoing.java

@WebServlet(urlPatterns = "/graphql")
public class GraphQLEndpoint extends SimpleGraphQLServlet {

public GraphQLEndpoint() {
    super(buildSchema());
}

//This method used SPQR
private static GraphQLSchema buildSchema() {
    GraphQLSchema schemaGenerator = new GraphQLSchemaGenerator()
            .withOperationsFromSingletons(myRepository) //register the beans
            .generate();
    return schemaGenerator;
}

 private static final MyRepository myRepository;

 static {
     myRepository= new MyRepository ();
 }
}

MyRepository.java

public class MyRepository{

private MyLibService libService;

 @GraphQLQuery
 public MyCayenneClass  find(Integer id) {
    List<MyCayenneClass> myList= libService.fetchById(new Integer[] {id});
    return myList.get(0);
 }
}

*供参考。如果我声明架构。代码可以正常工作

schema {
  query: Query
}

type Query {
  find(id: Int): MyCayenneClass
}

type ConcContract {    
 id: ID
  aProperty: Int
  anotherProperty: String        
}
4

1 回答 1

1

从 SPQR 的角度来看,这与 POJO 并没有什么不同,因为 SPQR 只关心类型。

默认情况下,对于所有嵌套类(MyCayenneClass在您的情况下),看起来像 getter 的所有内容都将被公开。对于顶级类(MyRepository在您的情况下),默认情况下仅公开带注释的方法。并且至少必须公开一个顶级方法,否则您的架构无效。

就目前而言,该错误仅意味着没有发现一个顶级查询。我看到@GraphQLQuery注释被注释掉了。这是故意的吗?使用默认配置,这不会公开任何查询。

如果您想公开未注释的方法,您可以注册不同的ResolverBuilder,例如(或您自己的实现/扩展)。PublicResolverBuilder

例如

generator.withOperationsFromSingleton(new MyRepository(), new PublicResolverBuilder())

这将公开该类的所有公共方法。

这是我尝试使用 v0.9.6 的一个稍微简化的示例,并且似乎可以按预期工作(我知道您使用的是错误文本中的一个相当旧的版本)。

public class MyRepository {

    @GraphQLQuery //not commented out
    public MyCayenneClass find(Integer in) {
        return new MyCayenneClass();
    }
}

// extends CayenneDataObject because I don't know where to get the 
// writeProperty and readProperty from
// but shouldn't change anything from SPQR's perspective
public class MyCayenneClass extends CayenneDataObject {
    public static final Property<Integer> A_PROPERTY = Property.create("aProperty", Integer.class);
    public static final Property<String> ANOTHER_PROPERTY = Property.create("anotherProperty", String.class);

    public void setAProperty(Integer aProperty) {
        writeProperty("aProperty", aProperty);
    }

    public Integer getAProperty() {
        return (Integer)readProperty("aProperty");
    }

    public void setAnotherProperty(String anotherProperty) {
        writeProperty("anotherProperty", anotherProperty);
    }

    public String getAnotherProperty() {
        return (String)readProperty("anotherProperty");
    }
}

您可以应用更多自定义项,具体取决于您最终需要什么,但从目前的问题来看,您似乎不需要任何额外的东西......

要覆盖ResolverBuilder用于嵌套类,您有 2 个选项。

1)全局注册它,所以所有嵌套类型都使用它:

generator.withNestedResolverBuilders(customBuilder)

2) 或按类型:

.withNestedResolverBuildersForType(MyCayenneClass.class, new BeanResolverBuilder())

但这很少需要......

于 2018-03-26T13:39:16.817 回答