我有一个使用React作为客户端框架的应用程序和一个用于运行API的Graphql。 计划正在获取用户帖子和帖子数。
客户端
const Profile = () => (
<div>
<PostCount />
<Posts />
</div>
)
const PostCount = () => ...
const Posts = () => ...
我们需要在Posts组件中显示帖子,并在Count组件中显示帖子数。 所以我的问题是,哪个更好。
在一个请求中获取Profile组件中的所有数据,并将其作为props发送到Post和Count组件。或者在Post组件中的Count组件和帖子中获取帖子数。
方案一包括一个服务器请求和更大的数据块。
方案二包括两个对服务器的请求和较小的数据块。
方案一服务器端:
const schema = `
type Query {
feeds(): Feed!
}
type Feed {
posts: [Post!]!
count: Int!
}
type Post {
...
}
`
async function feed() {
const posts: await Post.findAll();
const count = await Post.count();
return {
count
posts,
}
}
方案二服务器端:
const schema = `
type Query {
posts(): [Post!]!
count(): Int!
}
type Post {
...
}
`
async function feed() {
const posts: await Post.findAll();
return posts;
}
async function count() {
const count = await Post.count();
return count;
}
P.S。也考虑更大的数据。帖子和计数都是例子。例如用户帖子和用户评论。
答案 0 :(得分:1)
两种方式都是正确的!这取决于你的应用程序选择更好的方法!
public class MainActivity extends AppCompatActivity {
public int score = 0;
Button newRound;
TextView scoreText, highScore;
MediaPlayer sound = new MediaPlayer();
ColorStateList oldColors;
int hiScore = 0;
SharedPreferences preferences;
SharedPreferences.Editor editor;
ImageButton leaderboards;
int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
leaderboards = findViewById(R.id.leaderboards);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
enableTooltips();
}
preferences = PreferenceManager.getDefaultSharedPreferences(this);
editor = preferences.edit();
newRound = findViewById(R.id.button_gen);
scoreText = findViewById(R.id.score_tv);
highScore = findViewById(R.id.highScore);
oldColors = scoreText.getTextColors();
int savedScore = preferences.getInt("score", 0);
int savedHiScore = preferences.getInt("hiScore", 0);
scoreText.setText(""+savedScore);
highScore.setText("High score: "+savedHiScore);
score = savedScore;
hiScore = savedHiScore;
if(score >= 1){
changeColor();
}
newRound.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
newRound();
}
});
startSignInIntent();
}
@Override
protected void onStart() {
super.onStart();
}
private void startSignInIntent() {
GoogleSignInClient signInClient = GoogleSignIn.getClient(this,
GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN);
Intent intent = signInClient.getSignInIntent();
startActivityForResult(intent, RC_SIGN_IN);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
// The signed in account is stored in the result.
GoogleSignInAccount signedInAccount = result.getSignInAccount();
} else {
String message = result.getStatus().getStatusMessage();
if (message == null || message.isEmpty()) {
message = "Sign in error";
}
new AlertDialog.Builder(this).setMessage(message)
.setNeutralButton(android.R.string.ok, null).show();
}
}
}
通常比获取数据更快(有人可能搞砸了!:D),所以如果你单独获取它,你可以在帖子仍在加载时更快地显示它。
BTW,GQL自己处理count
!所以不需要异步等待!